类属性和方法

类可以包含属性和方法。

Property Let 允许最终用户为 private 类变量赋值。

Property Get 是一个只读属性,用于检索 private 类变量。

方法允许类执行用户所需的操作。方法不过是函数或子例程。

示例:

在下面的示例中,我们使用属性和方法来包装 private 变量。

Class Comp
    Private modStrType
    Public Property Let ComputerType(strType)
        modStrType = strType
    End Property
    Public Property Get ComputerType
        ComputerType = modStrType
    End Property
    Public 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" 'Let Property
str1 = objComp.ComputerType 'Get Property. Here, the value of str1 is Mac
objComp.Funct1
objComp.Sub1