Visual Basic 中的存取層級Access Levels in Visual Basic

已宣告專案的存取層級是存取它的能力範圍,也就是有哪些程式碼有權讀取或寫入它。The access level of a declared element is the extent of the ability to access it, that is, what code has permission to read it or write to it. 這不只是由您宣告專案本身的方式所決定,也是由專案之容器的存取層級所決定。This is determined not only by how you declare the element itself, but also by the access level of the element's container. 無法存取包含專案的程式碼無法存取其包含的任何專案,即使是宣告為 Public的專案也一樣。Code that cannot access a containing element cannot access any of its contained elements, even those declared as Public. 例如,您可以從包含結構的類別內部存取 Private 結構中的 Public 變數,而不是從該類別的外部進行存取。For example, a Public variable in a Private structure can be accessed from inside the class that contains the structure, but not from outside that class.

PublicPublic

宣告語句中的Public關鍵字會指定元素可以從相同專案中的程式碼、從參考專案的其他專案,以及從專案建立的任何元件中存取。The Public keyword in the declaration statement specifies that the element can be accessed from code anywhere in the same project, from other projects that reference the project, and from any assembly built from the project. 下列程式碼顯示範例 Public 宣告:The following code shows a sample Public declaration:

Public Class ClassForEverybody

您只能在模組、介面或命名空間層級使用 PublicYou can use Public only at module, interface, or namespace level. 這表示您可以在來源檔案或命名空間層級,或在介面、模組、類別或結構中宣告公用元素,但不能在程式中宣告。This means you can declare a public element at the level of a source file or namespace, or inside an interface, module, class, or structure, but not in a procedure.

ProtectedProtected

在宣告語句中, Protected關鍵字會指定只能從相同類別內或從這個類別衍生的類別來存取元素。The Protected keyword in the declaration statement specifies that the element can be accessed only from within the same class, or from a class derived from this class. 下列程式碼顯示範例 Protected 宣告:The following code shows a sample Protected declaration:

Protected Class ClassForMyHeirs

只有當您宣告類別的成員時,才可以在類別層級使用 ProtectedYou can use Protected only at class level, and only when you declare a member of a class. 這表示您可以在類別中宣告受保護的專案,但不能在來源檔案或命名空間的層級,或是在介面、模組、結構或程式內宣告。This means you can declare a protected element in a class, but not at the level of a source file or namespace, or inside an interface, module, structure, or procedure.

FriendFriend

在宣告語句中, Friend關鍵字會指定專案可以從相同的元件中存取,但不能從元件外部存取。The Friend keyword in the declaration statement specifies that the element can be accessed from within the same assembly, but not from outside the assembly. 下列程式碼顯示範例 Friend 宣告:The following code shows a sample Friend declaration:

Friend stringForThisProject As String

您只能在模組、介面或命名空間層級使用 FriendYou can use Friend only at module, interface, or namespace level. 這表示您可以在來源檔案或命名空間層級,或在介面、模組、類別或結構中宣告 friend 元素,但不能在程式中宣告。This means you can declare a friend element at the level of a source file or namespace, or inside an interface, module, class, or structure, but not in a procedure.

Protected FriendProtected Friend

在宣告語句中,受保護的 Friend關鍵字組合指定可以從衍生類別或從相同元件或兩者中存取專案。The Protected Friend keyword combination in the declaration statement specifies that the element can be accessed either from derived classes or from within the same assembly, or both. 下列程式碼顯示範例 Protected Friend 宣告:The following code shows a sample Protected Friend declaration:

Protected Friend stringForProjectAndHeirs As String

只有當您宣告類別的成員時,才可以在類別層級使用 Protected FriendYou can use Protected Friend only at class level, and only when you declare a member of a class. 這表示您可以在類別中宣告受保護的 friend 元素,但不能在來源檔案或命名空間的層級,或是在介面、模組、結構或程式內宣告。This means you can declare a protected friend element in a class, but not at the level of a source file or namespace, or inside an interface, module, structure, or procedure.

PrivatePrivate

宣告語句中的Private關鍵字會指定只能從相同的模組、類別或結構記憶體取元素。The Private keyword in the declaration statement specifies that the element can be accessed only from within the same module, class, or structure. 下列程式碼顯示範例 Private 宣告:The following code shows a sample Private declaration:

Private _numberForMeOnly As Integer

您只能在模組層級使用 PrivateYou can use Private only at module level. 這表示您可以在模組、類別或結構中宣告私用專案,但不能在來源檔案或命名空間的層級、介面內或程式中宣告該元素。This means you can declare a private element inside a module, class, or structure, but not at the level of a source file or namespace, inside an interface, or in a procedure.

在模組層級,不含任何存取層級關鍵字的 Dim 語句就相當於 Private 宣告。At the module level, the Dim statement without any access level keywords is equivalent to a Private declaration. 不過,您可能會想要使用 Private 關鍵字,讓您的程式碼更容易閱讀和解讀。However, you might want to use the Private keyword to make your code easier to read and interpret.

Private ProtectedPrivate Protected

宣告語句中的私用受保護關鍵字組合,會指定元素只能從相同的類別中存取,也可以從與包含類別相同的元件中找到的衍生類別進行存取。The Private Protected keyword combination in the declaration statement specifies that the element can be accessed only from within the same class, as well as from derived classes found in the same assembly as the containing class. 從 Visual Basic 15.5 開始支援 Private Protected 存取修飾詞。The Private Protected access modifier is supported starting with Visual Basic 15.5.

下列範例顯示 Private Protected 宣告:The following example shows a Private Protected declaration:

Private Protected internalValue As Integer

您只可以在類別內宣告 Private Protected 元素。You can declare a Private Protected element only inside of a class. 您不能在介面或結構內宣告它,也不能在來源檔案或命名空間層級(在介面或結構內)或程式中宣告它。You cannot declare it within an interface or structure, nor can you declare it at the level of a source file or namespace, inside an interface or a structure, or in a procedure.

Visual Basic 15.5 和更新版本支援 Private Protected 存取修飾詞。The Private Protected access modifier is supported by Visual Basic 15.5 and later. 若要使用它,請將下列專案新增至您的 Visual Basic 專案( *. vbproj)檔案。To use it, you add the following element to your Visual Basic project (*.vbproj) file. 只要您的系統上已安裝 Visual Basic 15.5 或更新版本,它就可讓您利用最新版本的 Visual Basic 編譯器所支援的所有語言功能:As long as Visual Basic 15.5 or later is installed on your system, it lets you take advantage of all the language features supported by the latest version of the Visual Basic compiler:

<PropertyGroup>
   <LangVersion>latest</LangVersion>
</PropertyGroup>

若要使用 Private Protected 的存取修飾詞,您必須將下列專案加入至您的 Visual Basic 專案( *. vbproj)檔案:To use the Private Protected access modifier, you must add the following element to your Visual Basic project (*.vbproj) file:

<PropertyGroup>
   <LangVersion>15.5</LangVersion>
</PropertyGroup>

如需詳細資訊,請參閱設定 Visual Basic 語言版本For more information see setting the Visual Basic language version.

存取修飾詞Access Modifiers

指定存取層級的關鍵字稱為存取修飾詞。The keywords that specify access level are called access modifiers. 下表比較存取修飾詞:The following table compares the access modifiers:

存取修飾詞Access modifier 授與的存取層級Access level granted 您可以使用此存取層級宣告的元素Elements you can declare with this access level 宣告內容,您可以在其中使用此修飾詞Declaration context within which you can use this modifier
Public LUnrestricted:

任何可以看到公用專案的程式碼都可以存取它Any code that can see a public element can access it
介面Interfaces

模組Modules

類別Classes

結構Structures

結構成員Structure members

程序Procedures

內容Properties

成員變數Member variables

常數Constants

列舉Enumerations

「事件」Events

外部宣告External declarations

委派Delegates
來源檔案Source file

命名空間Namespace

介面Interface

ModuleModule

執行個體Class

結構Structure
Protected Derivational:Derivational:

類別中宣告受保護專案的程式碼,或從中衍生的類別,可以存取元素Code in the class that declares a protected element, or a class derived from it, can access the element
介面Interfaces

類別Classes

結構Structures

程序Procedures

內容Properties

成員變數Member variables

常數Constants

列舉Enumerations

「事件」Events

外部宣告External declarations

委派Delegates
執行個體Class
Friend 組件︰Assembly:

宣告 friend 元素的元件中的程式碼可以存取它Code in the assembly that declares a friend element can access it
介面Interfaces

模組Modules

類別Classes

結構Structures

結構成員Structure members

程序Procedures

內容Properties

成員變數Member variables

常數Constants

列舉Enumerations

「事件」Events

外部宣告External declarations

委派Delegates
來源檔案Source file

命名空間Namespace

介面Interface

ModuleModule

執行個體Class

結構Structure
Protected FriendProtected Friend ProtectedFriend的聯集:Union of Protected and Friend:

相同類別或相同元件中的程式碼與受保護的 friend 元素相同,或在任何從專案類別衍生的類別中,都可以存取它Code in the same class or the same assembly as a protected friend element, or within any class derived from the element's class, can access it
介面Interfaces

類別Classes

結構Structures

程序Procedures

內容Properties

成員變數Member variables

常數Constants

列舉Enumerations

「事件」Events

外部宣告External declarations

委派Delegates
執行個體Class
Private 宣告內容:Declaration context:

宣告私用專案之類型中的程式碼(包括包含的類型內的程式碼)可以存取元素Code in the type that declares a private element, including code within contained types, can access the element
介面Interfaces

類別Classes

結構Structures

結構成員Structure members

程序Procedures

內容Properties

成員變數Member variables

常數Constants

列舉Enumerations

「事件」Events

外部宣告External declarations

委派Delegates
ModuleModule

執行個體Class

結構Structure
Private Protected 在類別中宣告私用受保護專案的程式碼,或在與 bas 類別相同的元件中找到的衍生類別中的程式碼。Code in the class that declares a private protected element, or code in a derived class found in the same assembly as the bas class. 介面Interfaces

類別Classes

結構Structures

程序Procedures

內容Properties

成員變數Member variables

常數Constants

列舉Enumerations

「事件」Events

外部宣告External declarations

委派Delegates
執行個體Class

請參閱See also