Objective-C中的成員變量有如下三種屬性函數
默認狀況下,是protected屬性spa
例子:類A有3個屬性,類B是類A的子類,main函數對於類A來講是外部。code
A.hblog
@interface A : NSObject { @public int publicVar; @protected int protectedVar; @private int privateVar; } @end
main.mit
int main(int argc, const char * argv[]) { @autoreleasepool { A *a = [[A alloc] init]; a->publicVar = 1; //OK a->protectedVar = 2; //Error 編譯不過 a->privateVar = 3; //Error 編譯不過 } return 0; }
B.mio
@implementation B - (void)f { B *b = [[B alloc] init]; b->publicVar = 1; //OK b->protectedVar = 2; //OK b->privateVar = 3; //Error 編譯不過 } @end
注意,這裏訪問的方式用的是【->】而不是【.】哦哦哦哦編譯