Person.h
#ifndef oc_person_h#define oc_person_h@interface person:nsobject{ int age; @protected float height;} -(int) age; Get Method-(void) Setage: (int) pAge; Set Method @end#endif
Person.m
#import <Foundation/Foundation.h> #import "Person.h" @implementation person-(int) age{ return age;} -(void) Setage: (int) page{Age = PAge;} @end
main.m
int main () { person* per = [[Person alloc] init]; int age = [per age]; Call the Get method [per setage:16];//Call the Set method //use "." To invoke Get/set using the original variable name, which requires the variable's get, set all conform to the convention int age2 = Per.age; Get per.age = n;//set return 0;}
every time this writing Get/set method, very troublesome, OC has an automated method that uses@protertyand the@synthesizeKey Words
Person.h
#ifndef oc_person_h#define oc_person_h@interface person:nsobject{ int age; @protected float height;} -(int) age;//-(void) Setage: (int) pAge; @property int age; The compiler automatically interprets the declaration of the Get/set method into int age @end#endif
person.m
#import <Foundation/Foundation.h> #import "Person.h" @implementation person//-(int) age//{// return age;//} -(void) Setage: (int) page//{//Age = page;//} @synthesize age;//compiler automatically interprets get, set method implementations of age @end
OC Language Learning (c) member variable Get/set method and "." Syntax, @proterty, and @synthesize keywords