Class 陳述式 (Visual Basic)Class Statement (Visual Basic)

宣告類別的名稱,並引進類別所組成之變數、屬性、事件和程式的定義。Declares the name of a class and introduces the definition of the variables, properties, events, and procedures that the class comprises.


[ <attributelist> ] [ accessmodifier ] [ Shadows ] [ MustInherit | NotInheritable ] [ Partial ] _  
Class name [ ( Of typelist ) ]  
    [ Inherits classname ]  
    [ Implements interfacenames ]  
    [ statements ]  
End Class  


詞彙Term 定義Definition
attributelist 選擇性。Optional. 請參閱屬性清單See Attribute List.
accessmodifier 選擇性。Optional. 可以是下列其中一項:Can be one of the following:

- 公用- Public
- 保護- Protected
- Friend- Friend
- - Private
- 受保護的 Friend- Protected Friend
- 私用保護- Private Protected

請參閱 Access levels in Visual BasicSee Access levels in Visual Basic.
Shadows 選擇性。Optional. 請參閱ShadowsSee Shadows.
MustInherit 選擇性。Optional. 請參閱MustInheritSee MustInherit.
NotInheritable 選擇性。Optional. 請參閱NotInheritableSee NotInheritable.
Partial 選擇性。Optional. 表示類別的部分定義。Indicates a partial definition of the class. 請參閱部分See Partial.
name 必要。Required. 這個類別的名稱。Name of this class. 請參閱 Declared Element NamesSee Declared Element Names.
Of 選擇性。Optional. 指定這是泛型類別。Specifies that this is a generic class.
typelist 如果您使用 of 關鍵字,則為必要項Required if you use the Of keyword. 這個類別的型別參數清單。List of type parameters for this class. 請參閱類型清單See Type List.
Inherits 選擇性。Optional. 表示這個類別會繼承另一個類別的成員。Indicates that this class inherits the members of another class. 請參閱Inherits 語句See Inherits Statement.
classname 如果您使用 Inherits 語句,則為必要。Required if you use the Inherits statement. 這個類別衍生來源的類別名稱。The name of the class from which this class derives.
Implements 選擇性。Optional. 表示這個類別會執行一或多個介面的成員。Indicates that this class implements the members of one or more interfaces. 請參閱Implements 語句See Implements Statement.
interfacenames 如果您使用 Implements 語句,則為必要。Required if you use the Implements statement. 這個類別所執行之介面的名稱。The names of the interfaces this class implements.
statements 選擇性。Optional. 定義此類別之成員的語句。Statements which define the members of this class.
End Class 必要。Required. 終止 Class 定義。Terminates the Class definition.


Class 語句會定義新的資料類型。A Class statement defines a new data type. 類別」(class)是物件導向程式設計(OOP)的基礎建立組塊。A class is a fundamental building block of object-oriented programming (OOP). 如需詳細資訊,請參閱物件和類別For more information, see Objects and Classes.

您只能在命名空間或模組層級使用 ClassYou can use Class only at namespace or module level. 這表示類別的宣告內容必須是原始程式檔、命名空間、類別、結構、模組或介面,而且不能是程式或區塊。This means the declaration context for a class must be a source file, namespace, class, structure, module, or interface, and cannot be a procedure or block. 如需詳細資訊,請參閱宣告內容和預設存取層級For more information, see Declaration Contexts and Default Access Levels.

類別的每個實例都有一個存留期,與所有其他實例無關。Each instance of a class has a lifetime independent of all other instances. 此存留期是由新的 Operator子句或函數(如 CreateObject)所建立。This lifetime begins when it is created by a New Operator clause or by a function such as CreateObject. 當所有指向實例的變數都已設定為「」或其他類別的實例時,它就會結束。It ends when all variables pointing to the instance have been set to Nothing or to instances of other classes.

類別預設為Friend存取。Classes default to Friend access. 您可以使用存取修飾詞來調整其存取層級。You can adjust their access levels with the access modifiers. 如需詳細資訊,請參閱Visual Basic 中的存取層級For more information, see Access levels in Visual Basic.


  • 嵌入.Nesting. 您可以在另一個類別中定義一個類別。You can define one class within another. 外部類別稱為「包含類別」,而內部類別稱為「嵌套類別」。The outer class is called the containing class, and the inner class is called a nested class.

  • 繼承Inheritance. 如果類別使用Inherits 語句,您只能指定一個基類或介面。If the class uses the Inherits Statement, you can specify only one base class or interface. 類別無法繼承自一個以上的元素。A class cannot inherit from more than one element.

    類別無法繼承自另一個具有更嚴格存取層級的類別。A class cannot inherit from another class with a more restrictive access level. 例如,Public 類別無法繼承自 Friend 類別。For example, a Public class cannot inherit from a Friend class.

    類別無法繼承自其內嵌套的類別。A class cannot inherit from a class nested within it.

  • 實作.Implementation. 如果類別使用Implements 語句,您就必須執行您在 interfacenames中指定的每個介面所定義的每個成員。If the class uses the Implements Statement, you must implement every member defined by every interface you specify in interfacenames. 例外狀況是基類成員的重新實作。An exception to this is reimplementation of a base class member. 如需詳細資訊,請參閱Implements中的 "重新實作"。For more information, see "Reimplementation" in Implements.

  • Default 屬性。Default Property. 類別最多可以指定一個屬性做為其預設屬性A class can specify at most one property as its default property. 如需詳細資訊,請參閱DefaultFor more information, see Default.


  • 存取層級。Access Level. 在類別中,您可以使用自己的存取層級來宣告每個成員。Within a class, you can declare each member with its own access level. 類別成員預設為公用存取,但變數和常數除外,預設為用存取。Class members default to Public access, except variables and constants, which default to Private access. 當類別的存取權比它的其中一個成員更受限制時,類別存取層級的優先順序會較高。When a class has more restricted access than one of its members, the class access level takes precedence.

  • 範圍.Scope. 類別會在其包含的命名空間、類別、結構或模組的範圍內。A class is in scope throughout its containing namespace, class, structure, or module.

    每個類別成員的範圍都是整個類別。The scope of every class member is the entire class.

    期.Lifetime. Visual Basic 不支援靜態類別。Visual Basic does not support static classes. 靜態類別的功能對等項是由模組所提供。The functional equivalent of a static class is provided by a module. 如需詳細資訊,請參閱Module 語句For more information, see Module Statement.

    類別成員有存留期,視宣告的方式和位置而定。Class members have lifetimes depending on how and where they are declared. 如需詳細資訊,請參閱Visual Basic 中的存留期For more information, see Lifetime in Visual Basic.

  • 加.Qualification. 類別外的程式碼必須使用該類別的名稱來限定成員名稱。Code outside a class must qualify a member's name with the name of that class.

    如果嵌套類別內的程式碼會對程式設計專案進行不合格的參考,Visual Basic 會先在嵌套類別中搜尋該專案,然後在其包含的類別中,依此類推,直到最外層的包含元素。If code inside a nested class makes an unqualified reference to a programming element, Visual Basic searches for the element first in the nested class, then in its containing class, and so on out to the outermost containing element.

類別和模組Classes and Modules

這些元素有許多相似之處,但也有一些重要的差異。These elements have many similarities, but there are some important differences as well.

  • 庫.Terminology. 舊版的 Visual Basic 會辨識兩種類型的模組:類別模組(cls 檔)和標準模組(bas 檔案)。Previous versions of Visual Basic recognize two types of modules: class modules (.cls files) and standard modules (.bas files). 目前的版本會分別呼叫這些類別模組The current version calls these classes and modules, respectively.

  • 共用成員。Shared Members. 您可以控制類別的成員是否為共用或實例成員。You can control whether a member of a class is a shared or instance member.

  • 物件方向。Object Orientation. 類別是物件導向,但模組則不是。Classes are object-oriented, but modules are not. 您可以建立一或多個類別的實例。You can create one or more instances of a class. 如需詳細資訊,請參閱物件和類別For more information, see Objects and Classes.


下列範例會使用 Class 語句來定義類別和數個成員。The following example uses a Class statement to define a class and several members.

Class BankAccount
    Shared interestRate As Decimal
    Private accountNumber As String
    Private accountBalance As Decimal
    Public holdOnAccount As Boolean = False

    Public ReadOnly Property Balance() As Decimal
            Return accountBalance
        End Get
    End Property

    Public Sub PostInterest()
        accountBalance = accountBalance * (1 + interestRate)
    End Sub

    Public Sub PostDeposit(ByVal amountIn As Decimal)
        accountBalance = accountBalance + amountIn
    End Sub

    Public Sub PostWithdrawal(ByVal amountOut As Decimal)
        accountBalance = accountBalance - amountOut
    End Sub
End Class

請參閱See also