class-variables

Using static function variable vs class variable to store some state

烂漫一生 提交于 2020-01-03 13:36:33
问题 Lets say I have function like this: void processElement() { doSomething(someArray[lastProcessedElement + 1]); } The thing is, every time this function called, I need to the store the last element that I called doSomething on. So in here I have two choices: I can create a private class variable named lastProcessedElement and increment it's value every time that function is called. This approach seems most common. So the code can be something like this: class Foo { int lastProcessedElement = 0;

Using static function variable vs class variable to store some state

ε祈祈猫儿з 提交于 2020-01-03 13:36:06
问题 Lets say I have function like this: void processElement() { doSomething(someArray[lastProcessedElement + 1]); } The thing is, every time this function called, I need to the store the last element that I called doSomething on. So in here I have two choices: I can create a private class variable named lastProcessedElement and increment it's value every time that function is called. This approach seems most common. So the code can be something like this: class Foo { int lastProcessedElement = 0;

Observing a value of a static var in a class?

你。 提交于 2020-01-02 06:33:30
问题 I have a class with a static var where the current online connection status is stored. I want to observe the value of ConnectionManager.online through other classes. I wanted to do this with KVO , but declaring a static variable as dynamic causes an error: class ConnectionManager: NSObject { dynamic static var online = false // adding 'dynamic' declaration causes error: // "A declaration cannot be both 'final' and 'dynamic' } What is a most elegant way of doing this? Update . This my code for

Java: Getting the properties of a class to construct a string representation

一笑奈何 提交于 2019-12-31 09:21:19
问题 Let's say I have a class like this (and also further assume that all the private variables: public class Item { private String _id = null; private String _name = null; private String _description = null; ... } Now, if I want to build a toString() representation of this class, I would do something like this inside the Item class: @Override public String toString() { return (_id + " " + _name + " " + _description); } But what if I have say 15 private variables inside the class? Do I have to

how to create class variable dynamically in python

时间秒杀一切 提交于 2019-12-31 08:58:12
问题 I need to make a bunch of class variables and I would like to do it by looping through a list like that: vars=('tx','ty','tz') #plus plenty more class Foo(): for v in vars: setattr(no_idea_what_should_go_here,v,0) is it possible? I don't want to make them for an instance (using self in the __init__) but as class variables. 回答1: You can run the insertion code immediately after a class is created: class Foo(): ... vars=('tx', 'ty', 'tz') # plus plenty more for v in vars: setattr(Foo, v, 0) Also

How can Ruby's attr_accessor produce class variables or class instance variables instead of instance variables?

天涯浪子 提交于 2019-12-28 08:11:41
问题 If I have a class with an attr_accessor , it defaults to creating an instance variable along with the corresponding getters and setters. But instead of creating an instance variable, is there a way to get it to create a class variable or a class instance variable instead? 回答1: Like this: class TYourClass class << self attr_accessor :class_instance_variable end end You can look at this as opening the metaclass of the class (of which the class itself is an instance) and adding an attribute to

Difference between class variables and class instance variables?

℡╲_俬逩灬. 提交于 2019-12-27 10:14:33
问题 Can anyone tell me about the difference between class variables and class instance variables? 回答1: A class variable ( @@ ) is shared among the class and all of its descendants. A class instance variable ( @ ) is not shared by the class's descendants. Class variable ( @@ ) Let's have a class Foo with a class variable @@i , and accessors for reading and writing @@i : class Foo @@i = 1 def self.i @@i end def self.i=(value) @@i = value end end And a derived class: class Bar < Foo end We see that

Difference between class variables and class instance variables?

十年热恋 提交于 2019-12-27 10:14:13
问题 Can anyone tell me about the difference between class variables and class instance variables? 回答1: A class variable ( @@ ) is shared among the class and all of its descendants. A class instance variable ( @ ) is not shared by the class's descendants. Class variable ( @@ ) Let's have a class Foo with a class variable @@i , and accessors for reading and writing @@i : class Foo @@i = 1 def self.i @@i end def self.i=(value) @@i = value end end And a derived class: class Bar < Foo end We see that

Perl objects - class variable initialization

痞子三分冷 提交于 2019-12-24 14:34:42
问题 I've just begun designing a Perl class, and my only prior experience with OOP is with C++, a long time ago. There are a few items of data that I need to be "class variables" - shared by all instances. I'd like for them to be initialized prior to the first time I instantiate an object, and I'd like for the main program that issues use MyClass to be able to provide a parameter for that initialization process. Here's a working example of a class with a class variable: package MyClass; use strict

Python Sqlalchemy - tablename as a variable

老子叫甜甜 提交于 2019-12-24 05:04:32
问题 I am using SQLAlchemy in Python and am declaring my classes inheriting from a declarative base as follows: from sqlalchemy import Column, Integer, String from sqlalchemy.ext.declarative import declarative_base Base = declarative_base() class SomeClass(Base): __tablename__ = 'some_table' id = Column(Integer, primary_key=True) name = Column(String(50)) As a user I would like to define the __tablename__ as a parameter, and not a hard-coded value , something like this: class SomeClass(Base): _