首页 > 其他分享 >OC之【@property的用法】

OC之【@property的用法】

时间:2022-12-09 15:01:06浏览次数:32  
标签:变量 no int age OC 用法 height property void

1.
这里的retain代表:在set方法中,release旧值,retain新值
(nonatomic, retain) Book *book;

(retain) Card *card;

代表只生成get方法的声明
默认是readwrite,同时生成get和set方法的声明
(readonly) int age;

// atomic就代表给方法进行加锁,保证线程安全
(atomic) int no;

// nonatomic代表方法不需要考虑线程安全问题
(nonatomic, assign) int no2;

是用来指定get方法的方法名
(nonatomic, getter = isRich) BOOL rich;

2.
Teacher

// 在xcode4.5以上的环境下,可以省略@synthesize,并且默认会去访问_age这个成员变量
// 如果找不到_age这个成员变量,会自动生成一个叫做_age的私有成员变量

-(void)test {

_age = 10;
}
@end

3.

#import "Student.h"

Student

// @synthesize age, height, no;

会自动生成getter和setter的实现

默认会去访问跟age同名的变量
// 如果找不到同名的变量,会自动生成一个私有的同名变量age
// @synthesize age;

代表getter和setter会去访问_age这个成员变量
age = _age;
//- (void)setAge:(int)newAge {
// _age = newAge;
//}
//
//- (int)age {
// return _age;
//}

@synthesize height = _height;
//- (void)setHeight:(float)newHeight {
// _height = newHeight;
//}
//
//- (float)height {
// return _height;
//}

no = _no;
//- (void)setNo:(int)newNo {
// _no = newNo;
//}
//
//- (int)no {
// return _no;
//}

- (void)test {

_age = 10;


_height = 10.0f;

_no = 10;

}
@end

标签:变量,no,int,age,OC,用法,height,property,void
From: https://blog.51cto.com/u_15907570/5925324

相关文章