iOS中四種執行個體變數的範圍類型@private@protected@public@package

來源:互聯網
上載者:User

標籤:

文檔上記錄是這樣的

The Scope of Instance Variables

To enforce the ability of an object to hide its data, the compiler limits the scope of instance variables—that is, limits their visibility within the program. 

為了強制一個對象隱藏其資料,編譯器限制執行個體變數範圍以限制其在程式中的可見度

But to provide flexibility, it also lets you explicitly set the scope at four levels. Each level is marked by a compiler directive:

但是為了提供靈活性,蘋果也讓開發人員顯式設定範圍(四選一)

Directive

Meaning

@private

The instance variable is accessible only within the class that declares it.

@protected

執行個體變數只能被聲明它的類訪問

The instance variable is accessible within the class that declares it and within classes that inherit it. All instance variables without an explicit scope directive have @protected scope.

執行個體變數能被聲明它的類和子類訪問,所有沒有顯式制定範圍的執行個體變數都是@protected

 

@public

The instance variable is accessible everywhere.

執行個體變數可以被在任何地方訪問。

 

@package

Using the modern runtime, an @package instance variable has @public scope inside the executable image that implements the class, but acts like @privateoutside.使用modern運行時,一個@package執行個體變數在實現這個類的可執行檔鏡像中實際上是@public的,但是在外面就是@private【runtime需要再看一下蘋果文檔Runtime Programming Guide】

The @package scope for Objective-C instance variables is analogous toprivate_extern for C variables and functions. Any code outside the class implementation’s image that tries to use the instance variable gets a link error.

Objective-C中的@package與C語言中變數和函數的private_extern類似。任何在實作類別的鏡像之外的代碼想使用這個執行個體變數都會引發link error

This scope is most useful for instance variables in framework classes, [email protected] may be too restrictive but @protected or @public too permissive.

這個類型最常用於架構類的執行個體變數,使用@private太限制,使用@protected或者@public又太開放

 

Objective-C 對存取許可權的設定。也是變數的範圍。

protected —Methods defined in the class and any subclasses can directly access the instance variables that follow.This is the default case. 該類和所有的子類中的方法可以直接存取這樣的變數,這是預設的。

private —Methods defined in the class can directly access the instance variables that follow, but subclasses cannot. 該類中的方法可以訪問這樣的變數,子類不可以。

public —Methods defined in the class and any other classes or modules can di- rectly access the instance variables that follow. 除了自己和子類中的方法外,也可以被其他類或者其他模組中的方法所訪問。開放性最大。

package —For 64-bit images, the instance variable can be accessed anywhere within the image that implements the class. 對於64位元影像像,這樣的成員變數可以在實現這個類的映像中隨意訪問。

 

經過研究,@package變數,對於framework內部,相當於@protected, 對於framework外部,相當於@privat。

這個特性,很適合用於開發第三方的靜態類庫,因為多數人並不希望讓別人知道自己屬性的值。

 

寫了一點代碼測試一下這個範圍:

@interface TestClass : NSObject {

@private
NSString *mNamePrivate;
@package
NSString *mTextPackage;

}

-(void)print;

@end

 

@implementation TestClass

-(id)init
{
self=[super init];
if (self) {
mNamePrivate = [[NSString alloc] initWithUTF8String:"mNamePrivate"];
mTextPackage = [[NSString alloc] initWithUTF8String:"mTextPrivate"];
}
return self;
}

-(void)print
{
NSLog(@"private:%@, package:%@", mNamePrivate, mTextPackage);
}

-(void)dealloc
{
[mNamePrivate release];
[mTextPackage release];
[super dealloc];
}

 

在其它的類裡面訪問package範圍的成員變數是可以的,需要使用->這種訪問方式。

TestClass *t = [[TestClass alloc] init];
[t print];
NSLog(@"%@", t->mTextPackage);//OK
NSLog(@"%@", t->mNamePrivate);//編譯錯誤,提示“instance variable ‘mNamePrivate‘ is private”
NSLog(@"%@", [t mNamePrivate]);//編譯提示“mNamePrivate找不到,”程式可以運行,但是會出錯
[t release];

 

iOS中四種執行個體變數的範圍類型@[email protected]@[email protected]

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.