instance-variables

Instance variable: self vs @

天涯浪子 提交于 2019-11-26 18:10:41
Here is some code: class Person def initialize(age) @age = age end def age @age end def age_difference_with(other_person) (self.age - other_person.age).abs end protected :age end What I want to know is the difference between using @age and self.age in age_difference_with method. Writing @age directly accesses the instance variable @age . Writing self.age tells the object to send itself the message age , which will usually return the instance variable @age — but could do any number of other things depending on how the age method is implemented in a given subclass. For example, you might have a

Local variables set to nil? (Objective-C)

风流意气都作罢 提交于 2019-11-26 18:09:15
问题 I'm reading a book on Objective-C and the author said that if local variables aren't assigned a value they will be set to nil, but static variables will be set to zero. So, I set up int a and didn't assign it a value. Then NSLog(@"%i", a) to display it and a was displayed as zero. I was a little confused on that and I was wondering if someone could clarify it for me? 回答1: With ARC enabled, your Objective-C object pointer variables will be set to nil regardless of where you create them.

What does @@variable mean in Ruby?

佐手、 提交于 2019-11-26 18:05:54
What are Ruby variables preceded with double at signs ( @@ )? My understanding of a variable preceded with an at sign is that it is an instance variable, like this in PHP: PHP version class Person { public $name; public function setName($name) { $this->name = $name; } public function getName() { return $this->name; } } Ruby equivalent class Person def set_name(name) @name = name end def get_name() @name end end What does the double at sign @@ mean, and how does it differ from a single at sign? A variable prefixed with @ is an instance variable , while one prefixed with @@ is a class variable .

Is there any reason to declare ivars if you're using properties exclusively in Objective-C?

巧了我就是萌 提交于 2019-11-26 17:54:42
I tend to use properties exclusively in my classes, especially now that you can declare properties in a class extension thanks to the modern Objective-C 2.0 runtime—I use this feature to create "private" properties. My question is if there is any good reason to ever declare ivars in a class interface anymore. I prefer my public-facing interfaces to be as minimal and clean as possible, only revealing aspects of my class that are pertinent. For example, I would tend to do the following: MyClass.h : @interface MyClass : NSObject @property (nonatomic, copy) NSString * publicString; @property

Total newbie: Instance variables in ruby?

心已入冬 提交于 2019-11-26 17:44:30
问题 Pardon the total newbiew question but why is @game_score always nil? #bowling.rb class Bowling @game_score = 0 def hit(pins) @game_score = @game_score + pins end def score @game_score end end 回答1: Let's walk through the code, shall we? #bowling.rb class Bowling @game_score = 0 # (1) At this point (1), we are still inside the class Bowling . Remember: classes are just objects like any other. So, at this point you are assigning 0 to the instance variable @game_score of the class object Bowling

Does a private @property create an @private instance variable?

ε祈祈猫儿з 提交于 2019-11-26 17:43:21
问题 I've read that @synthesize will automatically create corresponding instance variables for @property and that ivars are @protected by default. But, what if I use a class extension (like below) to indicate that the @property methods are to be private? // Photo.m @interface Photo () @property (nonatomic, retain) NSMutableData *urlData; @end Will the corresponding ivar then be @private ? Or should I explicitly declare it as @private like so? // Photo.h @interface Photo : Resource { @private

Static vs Instance Variables: Difference?

ぃ、小莉子 提交于 2019-11-26 15:23:20
What is the difference between a static and instance variable. The following sentence is what I cant get: In certain cases, only one copy of a particular variable should be shared by all objects of a class- here a static variable is used. A static variable represents class wide info.All objects of a class share the same data. I thought that instance vars were used class wide whereas static variables only had scope within their own methods? In the context of class attributes, static has a different meaning. If you have a field like: private static int sharedAttribute; then, each and every

Why are instance variables in Java always private?

一笑奈何 提交于 2019-11-26 13:04:00
问题 I\'m newbie to Java and I\'m learning about encapsulation and saw an example where instance variables are declared as private in a class. http://www.tutorialspoint.com/java/java_encapsulation.htm I have 2 queries: Why are instance variables private? Why not public? What if instance variables are made public and accessed directly? Do we see any constraints? Can you explain with an example as to what will go wrong in case the instance variables are declared as public in a class in Java? 回答1:

How to get instance variables in Python?

时光毁灭记忆、已成空白 提交于 2019-11-26 12:35:24
问题 Is there a built-in method in Python to get an array of all a class\' instance variables? For example, if I have this code: class hi: def __init__(self): self.ii = \"foo\" self.kk = \"bar\" Is there a way for me to do this: >>> mystery_method(hi) [\"ii\", \"kk\"] Edit: I originally had asked for class variables erroneously. 回答1: Every object has a __dict__ variable containing all the variables and its values in it. Try this >>> hi_obj = hi() >>> hi_obj.__dict__.keys() 回答2: Use vars() class

Can non-static methods modify static variables

試著忘記壹切 提交于 2019-11-26 10:29:34
问题 I am wondering how a non static method can modify a static variable. I know that static methods can only access other static methods and static variables. However, is the other side true? Can non-static methods access only non-static variables? For example: public class SampleClass { private static int currentCount = 0; public SampleClass() { currentCount++; } public void increaseCount() { currentCount++; } } This code compiles and I would like to know why in terms of static access privledges