權限問題
- 預設的權限是 @protected
- Java 實作的方式是在 methods 與變數前面加上 public/private/protected 修飾語,而 Objective-C 的作法則更像 C++ 對於 instance variable(C++ 術語 data members)的方式。
- Access.h
#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 = 10; printf( "public var: %i\n", a->publicVar ); // doesn't compile //a->privateVar = 100; //printf( "private var: %i\n", a->privateVar ); [a release]; return 0; }
- output
public var: 10
- C++ 中 private: [list of vars] public: [list of vars] 的格式,它只是改成了@private, @protected, 等等。
沒有留言:
張貼留言