#import <Foundation/Foundation.h>
@interface Person : NSObject
{
int _age;
int age;
}
//@property 可以自动生成某个成员变量的setter和getter声明
@property int age;
//- (void)setAge:(int)age;
//- (int)age;
//@property int _age;
//- (void) set_age:(int)_age;
//- (int)_age;
- (void)test;
@end
@implementation Person
//@synthesize自动生成age的setter和getter实现,并且会访问_age这个成员变量
//会访问_age这个成员变量,如果不存在,就会自动生成@private类型的_speed变量
@synthesize age = _age;
/*
- (void)setAge:(int)age
{
_age = age;
}
- (int)age
{
return _age;
}
*/
//@synthesize age; ///默认 成员变量是 age ,如果没有age,就会自动生成@private类型的age变量
- (void)test
{
NSLog(@"age=%d,_age=%d", age, _age);
}
@end
int main (int argc, const char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
Person *p = [Person new];
[p setAge:10];
[p test];
[pool drain];
return 0;
}
/*
自从Xcode 4.4后 ,@property 就拥有了 @synthesize的功能,也就是说,@property可以同时
生成setter和getter的声明和实现
默认情况下,setter和getter方法中的实现,会访问下划线 _ 开头的成员变量
*/
@property 和 @synthesize
最新推荐文章于 2024-02-12 22:25:20 发布
