property和synthesize
【一】
Student.h
--------------------------------------------
@interface Student : NSObject {
int number;
}
//生成getter和setter声明
@property int number
// -(void) setNumber:(int) newNumber;
//- (int) number;
@end
--------------------------------------------
--------------------------------------------
@implementation Student
//实际上编译器如果找不到这个变量,会生成一个同名变量(但是变量类型呢)
@synthesize number;
// -(void) setNumber:(int) newNumber
//{
// number = newNumber;
//}
//- (int) number
//{
// return number;
//}
@end
--------------------------------------------
【二】
--------------------------------------------
@interface Student : NSObject {
int _number;
}
//生成getter和setter声明
@property int number;
@end
--------------------------------------------
--------------------------------------------
@implementation Student
//实际上编译器如果找不到这个变量,会生成一个同名变量(但是变量类型呢)
//这样就不会生成number,getter和setter使用_number
@synthesize number = _number;
@end
————————————————
【三】
高版本Xcode,只需要@property 即可,可以省略@ synthesize
--------------------------------------------
@interface Student : NSObject
//生成getter和setter声明,并且生成_number变量(如果自己声明过_number,则就不会生成)
@property int number;
@end
-------------------------------------------