Public 和 Private 陳述式

物件導向程式設計的其中一個目標是提供黑箱,對一般使用者隱藏實作詳細資料。為了協助此複雜性封裝,BSL 可讓您隱藏物件的方法和特性。

若要建立對一般使用者隱藏的方法和特性,請在特性或方法的定義前加上 Private 關鍵字。

若要建立一般使用者可存取的方法和特性,請在特性或方法的定義前加上 Public 或 Dim 關鍵字。如果您未明確指定方法或特性應為 public、dim 或 private,則會將其設為 public。

不過,強烈建議您一律明確指出特性或方法是 Public 或 Private。

嘗試透過類別的例項存取專用方法或特性將會導致錯誤。

範例:

Class Comp
    Private modStrType
    Private Property Let ComputerType(strType)
        modStrType = strType
    End Property
    Public Property Get ComputerType
        ComputerType = modStrType
    End Property
    Private Function Funct1
        'Add business logic using class variables
    End Function
    Public Sub Sub1
        'Add business logic using class variables
    End Sub
End Class

Set objComp = new Comp
objComp.ComputerType = "Mac"        'Invalid, accessing this property will throw error as it’s private
str1 = objComp.ComputerType 
objComp.Funct1                           'Invalid, accessing this function will throw error as it’s private
objComp.Sub1