Shadows (Visual Basic)Shadows (Visual Basic)

指定宣告的程式設計項目會重新宣告並隱藏同名的項目或一組多載的項目,基底類別中。Specifies that a declared programming element redeclares and hides an identically named element, or set of overloaded elements, in a base class.

備註Remarks

遮蔽的主要目的 (也稱為依名稱隱藏) 是保留您的類別成員的定義。The main purpose of shadowing (which is also known as hiding by name) is to preserve the definition of your class members. 基底類別可能進行的變更,會建立具有相同名稱做為其中一個已定義的項目。The base class might undergo a change that creates an element with the same name as one you have already defined. 如果發生這種情況,Shadows修飾詞強制參考透過您的類別成員解析您定義的而不是新的基底類別項目。If this happens, the Shadows modifier forces references through your class to be resolved to the member you defined, instead of to the new base class element.

遮蔽和覆寫都會重新定義繼承的項目,但這兩種方法之間有顯著的差異。Both shadowing and overriding redefine an inherited element, but there are significant differences between the two approaches. 如需詳細資訊,請參閱 < Visual Basic 中的遮蔽功能For more information, see Shadowing in Visual Basic.

規則Rules

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

    您可以宣告一個宣告陳述式中只能有一個遮蔽的項目。You can declare only one shadowing element in a single declaration statement.

  • 結合的修飾詞。Combined Modifiers. 您無法指定Shadows連同OverloadsOverrides,或Static相同宣告中。You cannot specify Shadows together with Overloads, Overrides, or Static in the same declaration.

  • 項目型別。Element Types. 您可以使用任何其他類型遮蔽任何一種已宣告的項目。You can shadow any kind of declared element with any other kind. 如果您遮蔽的屬性或程序與另一個屬性或程序,參數和傳回型別沒有以符合那些在基底類別屬性或程序。If you shadow a property or procedure with another property or procedure, the parameters and the return type do not have to match those in the base class property or procedure.

  • 存取。Accessing. 無法從遮蔽的衍生類別內通常使用遮蔽基底類別中的項目。The shadowed element in the base class is normally unavailable from within the derived class that shadows it. 不過,適用下列考量。However, the following considerations apply.

    • 如果遮蔽的項目不能從參考它的程式碼存取,則參考會解析為遮蔽的項目中。If the shadowing element is not accessible from the code referring to it, the reference is resolved to the shadowed element. 例如,如果Private項目會遮蔽基底類別的項目,並沒有存取權限的程式碼Private項目改為存取基底類別項目。For example, if a Private element shadows a base class element, code that does not have permission to access the Private element accesses the base class element instead.

    • 如果遮蔽的項目時,您仍然可以透過使用基底類別的型別宣告的物件存取遮蔽的項目。If you shadow an element, you can still access the shadowed element through an object declared with the type of the base class. 您也可以存取透過MyBaseYou can also access it through MyBase.

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

另請參閱See also