本帖最后由 jiangenhao 于 2014-5-9 19:15 编辑
- 1.@property和@synthesize
- #import <Foundation/Foundation.h>
- @interface Person : NSObject
- {
- int _age;
- int age;
- int _height;
- int height;
- int _weight;
- int weight;
- int _money;
- int money;
- }
- @property int age;
- @property int height;
- @property int weight;
- @property int money;
- - (void)test;
- @end
- @implementation Person
- @synthesize height = _height;
- @synthesize weight;
- - (void)setMoney:(int)money
- {
- self->money = money;
- }
- - (int)height
- {
- return 180;
- }
- - (int)age
- {
- return age;
- }
- - (void)test
- {
- NSLog(@"age=%d, _age=%d, self.age=%d", age, _age, self.age);
- NSLog(@"height=%d, _height=%d, self.height=%d", height, _height, self.height);
- NSLog(@"weight=%d, _weight=%d, self.weight=%d", weight, _weight, self.weight);
- NSLog(@"money=%d, _money=%d, self.money=%d", money, _money, self.money);
- }
- @end
- int main()
- {
- Person *p = [Person new];
- p.age = 10;
- p.weight = 50;
- p.height = 160;
- p.money = 2000;
- [p test];
- return 0;
- }
复制代码
请问self.age输出结果为什么为0 |