setter getter 方法
-(void)setWidth:(double)aWidth;
-(double) width;
-(void)setHeight:(double)aHeight;
-(double) height;
//[rectangle setWidth:10];
//NSLog(@“%g”,[rectangle width];
rectangle.width = 10;
NSLog(@"%g",rectangle.width);//读取可以用点语法
//只能用来读取数据
set方法自定义后,系统会调用自定义的方法而不是属性
属性
声明
@property(nonatomic,assign)double width;
@property(nonatomic,assign)double height;
@property(nonatomic,retain)NSString* name;
demo.m
@synthesize width,height,name;
//变量声明可不写
//但有时为了实例变量的安全
@property int age;
@synthesize age = _age;//前者为属性名,后者为变量名
------------------
//访问方法
-(void)set{
self.age = 10;
_age = 10;}
类外 object.age
//更改存取器方法的名字
@property(nonatomic,setter=setNO,getter=getNO:)double width;
[rectangle setNO:10];
NSLog(@“%g”,[rectangle getNO];
属性说明
线程相关
原子性 atomic(默认) //加锁,多线程
非原子性 nonatomic //常用
读写属性
readonly //只产生getter
readwrite(默认) //读写
引用计数(详见内存管理)
retain //对象,引用计数+1
assign //实例变量