In Objective-c, we can use new simple instead of alloc init, we present today is similar to the simple use of new OC features, with the @property, @synthesize to replace the Get,set method, it is very simple to use. Can save a lot of code, when the need to use the Set,get method of the place, we can use @property, @synthesize to a simple substitution, then the system will automatically give us the Set,get method of generating the variable, @property the declaration part of the corresponding method, @ Synthesize the implementation part of the corresponding method.
Human.h:
[Plain]View Plaincopy < param name= "wmode" value= "Transparent" >
- #import <Foundation/Foundation.h>
- @interface Human:nsobject
- {
- int age;
- float height;
- }
- Here, the declaration of the Set,get method representing the age and height is represented by @property.
- @property int age;
- @property float height;
- @end
HUMAN.M:
[Plain]View Plaincopy < param name= "wmode" value= "Transparent" >
- #import "Human.h"
- @implementation Human
- Here, @synthesize is used to represent the implementation of the Set,get method of the Age,height variable.
- @synthesize age;
- @synthesize height;
- @end
MAIN.M:
[Plain]View Plaincopy
- #import <Foundation/Foundation.h>
- #import "Human.h"
- int main (int argc, const char * argv[])
- {
- NSAutoreleasePool *pool=[[nsautoreleasepool Alloc]init];
- Human *human = [[Human alloc]init];
- “.” To the left of the equals sign, equivalent to the Set method.
- human.age=20; equivalent to [Human setage:20]
- human.height=60.50;
- “.” To the right of the equals sign, equivalent to the Get method.
- Float tmp=human.height;//equivalent to float TMP = [human height]
- NSLog (@ "Age%d, height%f", human.age,tmp);
- [Human release];
- [Pool release];//is equivalent to performing one release for each object in the pool;
- }
Output statement:
2012-03-15 10:11:34.052 string[325:403] Age , height 60.500000
At this time the system will automatically generate Age,height Set,get method, can be used directly, there is a situation, if I only want to let age have get method, there is no set method to do, that is, in the initialization of a default value, and this value can only be called, cannot be modified, Can you use @property this way? The answer is yes, because @property provides us with a similar parameter of an optional, the key word is readonly,readwrite, respectively, corresponding to only read can not modify and read and write can, generally we do not set ReadWrite, because the default is read and write state. Look at the code:
Human.h:
[Plain]View Plaincopy < param name= "wmode" value= "Transparent" >
- #import <Foundation/Foundation.h>
- @interface Human:nsobject
- {
- int age;
- float height;
- }
- Here, the declaration of the Set,get method representing the age and height is represented by @property.
- @property (readonly) int age;
- @property float height;
- @end
HUMAN.M:
[Plain]View Plaincopy < param name= "wmode" value= "Transparent" >
- #import "Human.h"
- @implementation Human
- Override the Init method to assign an initial value to age
- -(ID) init
- {
- if (Self=[super init])
- {
- age=20;
- }
- return self;
- }
- Here, @synthesize is used to represent the implementation of the Set,get method of the Age,height variable.
- @synthesize age;
- @synthesize height;
- @end
MAIN.M:
[Plain]View Plaincopy
- #import <Foundation/Foundation.h>
- #import "Human.h"
- int main (int argc, const char * argv[])
- {
- NSAutoreleasePool *pool=[[nsautoreleasepool Alloc]init];
- Human *human = [[Human alloc]init];
- human.height=60.5;
- human.age=30; If the line does not comment, it will be an error, that is, age only get method, there is no set method
- NSLog (@ "Age age=%d", human.age);
- [Human release];
- [Pool release];//is equivalent to performing one release for each object in the pool;
- }
The usage of @property @synthesize in Object-c