面向对象编程的目标之一是提供一个黑盒,对最终用户隐藏实施详细信息。为了帮助进行这种复杂性封装,BSL 允许您隐藏对象的方法和属性。
要创建对最终用户隐藏的方法和属性,请在属性或方法定义前面添加 Private 关键字。
要创建最终用户可以访问的方法和属性,请在属性或方法定义之前添加 Public 或 Dim 关键字。如果未显式指定方法或属性应为 public、dim 还是 private,则将其设置为 public。
但是,强烈建议您始终显式指示属性或方法是 Public 还是 Private。
尝试通过 Class 的实例访问 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