Create a new "Cocoa Touch Class" file named people
People.h Write
@interface People:nsobject@property int Age ; @end
PEOPLE.M write (in fact automatically generated)
@implementation people @end
Use occasions:
intMainintargcChar*argv[]) {People*p=[[People alloc]init]; P.age=Ten;//using the Set method directlyNSLog (@"Age %d\n", p.age);//using the Get method directly[P Setage: -];//calling the Set methodNSLog (@"Age %d\n", [P age]);//call the Get method inttest; Test=1; NSLog (@"Age %d\n", test); }
Manual Write Set/get method
The Set/get method can be generated automatically by @property in the example above, and we can also write the Set/get method manually.
Modify People.h
@interface people:nsobject{ int _age;} @property int age ; @end
Modify PEOPLE.M
@implementation People-(void) Setage: (int) age{ NSLog (@ "setage" ); _age= Age;} -(int) age{ NSLog (@ "getAge"); return _age;} @end
Through the personality, and then execute the program, found that we manually write the Set/get method of the output of the log.
OC Set/get method