I'm just learning OOP from a book I took (Big Nerd Ranch), and it just went through the head of the getter and setter. I just wanted to clarify, I understand what I just did. Instead of creating a method to set the value of the instance, and then another method to extract the value and display it, I create using the syntax @property and @synthesize to define both methods.
Instead of this:
-(void) setHeightOfObject:(int)h; -(void) setWeightOfObject:(float)w; -(int) heightOfObject; -(float) weightOfObject;
and defining it as follows:
- (int)heightOfObject { return heightOfObject; } - (void)setHeightOfObject:(int)h { heightInMeters = h; } - (float)weightOfObject { return weightOfObject; } - (void)setWeightOfObject:(float)w { weightOfObject = w; }
I would do this with getter and seters in a .h file:
@property int heightOfObject; @property float weightOfObject;
And then go to my .m file and link it:
@synthesize heightInMeters, weightOfObject;
Does this give me the opportunity to set the value of my object and then get it if I need to print it? I know this is an important concept, and I want to make sure that I understand it correctly.
David source share