Protected (Visual Basic)Protected (Visual Basic)

成員存取修飾詞,指定一或多個宣告的程式設計項目,只能從其本身的類別或衍生類別中存取。A member access modifier that specifies that one or more declared programming elements are accessible only from within their own class or from a derived class.

備註Remarks

有時候,在類別中宣告的程式設計專案包含敏感性資料或受限制的程式碼,而您想要限制對專案的存取。Sometimes a programming element declared in a class contains sensitive data or restricted code, and you want to limit access to the element. 不過,如果類別是可繼承的,而且您預期衍生類別的階層,則這些衍生類別可能需要存取資料或程式碼。However, if the class is inheritable and you expect a hierarchy of derived classes, it might be necessary for these derived classes to access the data or code. 在這種情況下,您會想要從基類和所有衍生類別來存取元素。In such a case, you want the element to be accessible both from the base class and from all derived classes. 若要以這種方式限制對專案的存取,您可以使用 Protected來宣告該元素。To limit access to an element in this manner, you can declare it with Protected.

注意

Protected 存取修飾詞可以與兩個其他修改者結合:The Protected access modifier can be combined with two other modifiers:

  • 受保護的 Friend修飾詞可讓類別成員從該類別、衍生類別,以及定義類別的相同元件中存取。The Protected Friend modifier makes a class member accessible from within that class, from derived classes, and from the same assembly in which the class is defined.
  • 私用保護的修飾詞可讓衍生類型存取類別成員,但只能在其包含的元件內使用。The Private Protected modifier makes a class member accessible by derived types, but only within its containing assembly.

規則Rules

宣告內容。Declaration Context. 您只能在類別層級使用 ProtectedYou can use Protected only at the class level. 這表示 Protected 元素的宣告內容必須是類別,而且不能是原始程式檔、命名空間、介面、模組、結構或程式。This means the declaration context for a Protected element must be a class, and cannot be a source file, namespace, interface, module, structure, or procedure.

行為Behavior

  • 存取層級。Access Level. 類別中的所有程式碼都可以存取其元素。All code in a class can access its elements. 任何衍生自基類的類別中的程式碼,都可以存取基類的所有 Protected 元素。Code in any class that derives from a base class can access all the Protected elements of the base class. 這適用于所有的衍生層代。This is true for all generations of derivation. 這表示類別可以存取基類基類(base class)的 Protected 元素,依此類推。This means that a class can access Protected elements of the base class of the base class, and so on.

    受保護的存取不是 friend 存取的超集合或子集。Protected access is not a superset or subset of friend access.

  • 存取修飾詞。Access Modifiers. 指定存取層級的關鍵字稱為存取修飾詞。The keywords that specify access level are called access modifiers. 如需存取修飾詞的比較,請參閱Visual Basic 中的存取層級For a comparison of the access modifiers, see Access levels in Visual Basic.

Protected 修飾詞可用於以下內容:The Protected modifier can be used in these contexts:

請參閱See also