PRIVATE:
只能由两类函数访问:1,当前类的成员函数;2,友元函数
PROTECTED:
只能由三类函数访问:1,当前类的成员函数;2,子类的成员函数;3,友元函数
PUBLIC:
可以由四类事物访问:1,当前类的成员函数;2,子类的成员函数;3,友元函数;4,当前类的实例
-
#import <Foundation/NSObject.h> @interface Access: NSObject { @public int publicVar; @private int privateVar; int privateVar2; @protected int protectedVar; } @end
- Access.m
#import "Access.h" @implementation Access @end
- main.m
#import "Access.h" #import <stdio.h> int main( int argc, const char *argv[] ) { Access *a = [[Access alloc] init]; // works a->publicVar = 5; printf( "public var: %i\n", a->publicVar ); // doesn't compile //a->privateVar = 10; //printf( "private var: %i\n", a->privateVar ); [a release]; return 0; }