- @public 修飾的實(shí)例變量在類(lèi)外部有
->訪問(wèn) - 定義一個(gè)屬性會(huì)自動(dòng)有如下規(guī)則
1.自動(dòng)生成訪問(wèn)方法
2.自動(dòng)生成實(shí)例變量
3.可以更簡(jiǎn)單地調(diào)用訪問(wèn)方法 - @synthessize后面只能跟屬性,不能跟實(shí)例變量,并為屬性生成getter,setter方法
//屬性的實(shí)現(xiàn)
@synthesize brand=(brand), price(=price), color; //屬性的實(shí)現(xiàn)可以省略
//自動(dòng)生成一個(gè)實(shí)例變量brand... 綁定到相應(yīng)的屬性brand...上
//2.setter和getter方法對(duì)實(shí)例變量brand進(jìn)行操作
如果不寫(xiě)屬性的實(shí)現(xiàn)
//如果不寫(xiě)屬性的實(shí)現(xiàn), 系統(tǒng)默認(rèn)補(bǔ)上的形式
@synthesize brand = _brand, price = _price, color = _color;
//1.自動(dòng)生成一個(gè)實(shí)例變量_brand
//2.setter和getter方法對(duì)實(shí)例變量_brand進(jìn)行操作
- @dynamic關(guān)鍵字修飾告訴編譯器屬性自動(dòng)生成的setter,getter方法無(wú)效,用戶自定義訪問(wèn)方法。
- 當(dāng)重寫(xiě)了屬性的setter,getter方法后,系統(tǒng)默認(rèn)補(bǔ)上的
@synthesize age = _age就失效了
//當(dāng)同時(shí)重寫(xiě)setter和getter后, @synthesize age = _age; 就失效了
//解決方案:自己補(bǔ)上 @synthesize age = _age
@synthesize age = _age;
//當(dāng)屬性生成的setter方法不滿足需求時(shí), 可以重寫(xiě)setter方法
- (void)setAge:(NSInteger)age {
if (age > _age) {
_age = age;
}
}
//當(dāng)屬性生成的getter方法不滿足需求時(shí), 可以重寫(xiě)getter方法
- (NSInteger)age {
return _age - 2;
}