成員變量,實(shí)例變量,屬性變量
@interface MyViewController :UIViewControlle
{
UIButton *yourButton;
int count;
id data;
}
@property (nonatomic, strong) UIButton *myButton;
@end
成員變量 (實(shí)例變量 + 基本數(shù)據(jù)類型)
UIButton *yourButton;,int count;,id data;,實(shí)例變量
UIButton *yourButton;,id data;屬性
@property (nonatomic, strong) UIButton *myButton;
成員變量 可以加變量修飾詞
@public 、@protected、@package、@private
-
@public任何地方都可以訪問 如外部訪問: person->name -
@package框架內(nèi)有效 可以外部訪問: -
@protected默認(rèn)就是這個(gè)修飾詞, 只能在 自己和子類中訪問 -
@private私有限制,只能在自己類中訪問。
類別中的屬性property
類與類別中添加的屬性要區(qū)分開來,因?yàn)轭悇e中只能添加方法,不能添加實(shí)例變量。經(jīng)常會(huì)在ios的代碼中看到在類別中添加屬性,這種情況下,是不會(huì)自動(dòng)生成實(shí)例變量的,必須自己實(shí)現(xiàn) get/set 方法。
注意一點(diǎn),匿名類別(匿名擴(kuò)展)是可以添加實(shí)例變量的,非匿名類別是不能添加實(shí)例變量的,只能添加方法,或者屬性(其實(shí)也是方法)。