OC点语法的陷阱
1 #import <Foundation/Foundation.h> 2 3 //类的声明 4 @interface Person : NSObject{ 5 NSString *_name;//属性 6 } 7 - (void)setName:(NSString *)name;//setter 8 - (NSString *)name;//getter 9 @end 10 11 //类的实现 12 @implementation Person 13 - (void)setName:(NSString *)name{ 14 //self.name=name;//点语法陷阱,相当于调用setName 15 _name=name; 16 } 17 - (NSString *)name{ 18 //return self.name;//点语法陷阱,也是重复调用了getter方法 19 return _name; 20 } 21 @end 22 23 int main(int argc, const char * argv[]) { 24 @autoreleasepool { 25 Person *p=[[Person alloc]init]; 26 p.name=@"张三";//赋值 27 NSLog(@"%@",p.name);//读取值 28 } 29 return 0; 30 }