//1.@property和@synthesize #import <Foundation/Foundation.h>
//创建Person这个类 @interface Person : NSObject { int _age; //年龄 int age;
int _height; //身高 int height;
int _weight; //体重 int weight;
int _money; //钱 int money; }
//各种getter和setter声明 @property int age; @property int height; @property int weight; @property int money;
//test的声明 - (void)test; @end
@implementation Person @synthesize height = _height; //_height的getter和setter实现 @synthesize weight; //weight的getter和setter实现
- (void)setMoney:(int)money //money的set方法 { self->money = money; }
- (int)height //height的get方法 { return 180; }
- (int)age //age的get方法 { return age; }
- (void)test { NSLog(@"age=%d, _age=%d, self.age=%d", age, _age, self.age); //age = 10,_age = 0,self.age = 10; NSLog(@"height=%d, _height=%d, self.height=%d", height, _height, self.height); //height = 0,_height = 160,self.height = 160 NSLog(@"weight=%d, _weight=%d, self.weight=%d", weight, _weight, self.weight); //weight=50,_weight=0,self.wight=50 NSLog(@"money=%d, _money=%d, self.money=%d", money, _money, self.money); //money=2000,_money=0,self.money=2000 } @end
int main() { Person *p = [Person new]; p.age = 10; //age = 10 p.weight = 50; //weight = 50 p.height = 160; // _height = 160 p.money = 2000; //money = 2000 [p test]; return 0; } 输出为什么是 age=0, _age=10, self.age=0 height=0, _height=160, self.height=180 weight=50, _weight=0, self.weight=50 money=2000, _money=0, self.money=0 而不是我上面所解的呢???
|