[code]
\' 定义Person类
Public Class Person
Private mName As String
Private mAge As Integer
\' 定义Name属性
Public Property Let Name(ByVal value As String)
mName = value
End Property
Public Property Get Name() As String
Name = mName
End Property
\' 定义Age属性
Public Property Let Age(ByVal value As Integer)
mAge = value
End Property
Public Property Get Age() As Integer
Age = mAge
End Property
\' 定义Introduction方法
Public Function Introduction() As String
Introduction = \"My name is \" & mName & \", and I am \" & mAge & \" years old.\"
End Function
End Class
[/code]
在VB中,定义类需要使用Class关键字。下面举一个简单的例子来说明如何在VB中定义类:定义一个名为Person的类,包含Name、Age、Introduction等成员:\\\' 定义Person类Public Class Person Private mName As String Private mAge As Integer \\\' 定义Name属性 Public Property Let Name(ByVal value As String) mName = value End Property Public Property Get Name() As String Name = mName End Property \\\' 定义Age属性 Public Property Let Age(ByVal value As Integer) mAge = value End Property Public Property Get Age() As Integer Age = mAge End Property \\\' 定义Introduction方法 Public Function Introduction() As String Introduction = \\\"My name is \\\" & mName & \\\", and I am \\\" & mAge & \\\" years old.\\\" End FunctionEnd Class 在类模块中,使用Class关键字定义类,并定义类的成员。可以定义公共成员(Public)和私有成员(Private),包括属性、方法、事件等。Public关键字表示该成员是公共的,可以从类外部访问;Private关键字表示该成员是私有的,只能从类内部访问。例如,在上面的代码中,我们定义了两个私有成员mName和mAge,以及三个公共成员Name、Age和Introduction。其中,Name和Age是属性,可以设置和获取对象的姓名和年龄;Introduction是函数,返回人物介绍信息。