让一个类成为另一个类的属性

‘一、建立子类 CAddress.cls
Private m_Street As String
Private m_City As String

Public Property Get Street() As String
Street = m_Street
End Property

Public Property Let Street(ByVal sStreet As String)
m_Street = sStreet
End Property

Public Property Get City() As String
City = m_City
End Property

Public Property Let City(ByVal sCity As String)
m_City = sCity
End Property

‘ 二、建立父类 CPerson.cls
Private m_Address As CAddress ‘ 声明 CAddress 类型的私有变量

‘ 自动初始化 CAddress 实例
Private Sub Class_Initialize()
Set m_Address = New CAddress
End Sub

‘ 清理资源
Private Sub Class_Terminate()
Set m_Address = Nothing
End Sub

‘ 通过 Property Get 公开 CAddress 实例
Public Property Get Address() As CAddress
Set Address = m_Address
End Property

‘ 可选:允许外部设置 CAddress 实例
Public Property Set Address(ByVal objAddress As CAddress)
Set m_Address = objAddress
End Property

‘ 三、在窗体或模块中使用:
Dim person As CPerson
Set person = New CPerson

‘ 设置地址属性
With person.Address
.Street = “123 Main St”
.City = “New York”
End With

‘ 读取地址属性
MsgBox “地址:” & person.Address.Street & “, ” & person.Address.City

‘四、注意事项

Set person.Address = New CAddress ‘ 正确
person.Address = New CAddress ‘ 错误(缺少 Set)

齐国

齐国(前1044年至前221年):姜姓。西周立国后,周武王封姜尚于营丘(后改临淄),建立齐国。齐国乃是周朝时的东方大国,春秋末期时发生“田氏代齐”,姜齐被田齐所取代,并成为战国七雄之一。齐湣王时,齐国因频繁对外用兵,惨遭“五国伐齐”,齐国就此衰弱。秦王政二十六年(前221年),秦国攻灭其他五国后,攻入齐国,齐王建出城投降,齐国灭亡,秦国一统天下。

鲁国

鲁国(前1043年至前255年):姬姓。周武王灭商后,将少昊之墟封给了周公,定国号为鲁。因周公旦要留在镐京辅佐年幼的周成王,于是便派儿子伯禽就封,周成王则下令将奄国和“殷民六族”同时赐予伯禽。鲁国先后传承795年,历三十四位君主,最终于鲁顷公二十四年(前255年)被楚考烈王所灭。

建立类模块

‘ 一、类模块代码

‘ 类名: clsWire
‘ 包含8个参数、2个方法、1个事件

Option Explicit

‘ —————————-
‘ 1. 定义私有变量(8个参数)
‘ —————————-
Private pMaterial As String ‘ 材料
Private pLength As Double ‘ 长度
Private pDiameter As Double ‘ 直径
Private pVoltage As Double ‘ 电压
Private pCurrent As Double ‘ 电流
Private pResistance As Double ‘ 电阻
Private pTemperature As Double ‘ 温度
Private pIsInsulated As Boolean ‘ 是否有绝缘层

‘ —————————-
‘ 2. 定义事件
‘ —————————-
Event StatusChanged(ByVal Message As String)

‘ —————————-
‘ 类初始化(设置默认值)
‘ —————————-
Private Sub Class_Initialize()
pMaterial = “Copper” ‘ 默认材料为铜
pLength = 0
pDiameter = 0
pVoltage = 0
pCurrent = 0
pResistance = 0
pTemperature = 25 ‘ 默认温度25°C
pIsInsulated = False
End Sub

‘ —————————-
‘ 属性定义(8个参数)
‘ —————————-
Public Property Get Material() As String
Material = pMaterial
End Property
Public Property Let Material(ByVal Value As String)
pMaterial = Value
End Property

Public Property Get Length() As Double
Length = pLength
End Property
Public Property Let Length(ByVal Value As Double)
pLength = Value
End Property

Public Property Get Diameter() As Double
Diameter = pDiameter
End Property
Public Property Let Diameter(ByVal Value As Double)
pDiameter = Value
End Property

Public Property Get Voltage() As Double
Voltage = pVoltage
End Property
Public Property Let Voltage(ByVal Value As Double)
pVoltage = Value
End Property

Public Property Get Current() As Double
Current = pCurrent
End Property
Public Property Let Current(ByVal Value As Double)
pCurrent = Value
End Property

Public Property Get Resistance() As Double
Resistance = pResistance
End Property
Public Property Let Resistance(ByVal Value As Double)
pResistance = Value
End Property

Public Property Get Temperature() As Double
Temperature = pTemperature
End Property
Public Property Let Temperature(ByVal Value As Double)
pTemperature = Value
End Property

Public Property Get IsInsulated() As Boolean
IsInsulated = pIsInsulated
End Property
Public Property Let IsInsulated(ByVal Value As Boolean)
pIsInsulated = Value
End Property

‘ —————————-
‘ 方法1:计算功率
‘ —————————-
Public Function CalculatePower() As Double
CalculatePower = pVoltage * pCurrent ‘ P = V * I
RaiseEvent StatusChanged(“功率计算完成: ” & CalculatePower & “W”)
End Function

‘ —————————-
‘ 方法2:检查安全性
‘ —————————-
Public Sub CheckSafety()
Dim msg As String
If pTemperature > 80 Then
msg = “警告:温度过高(” & pTemperature & “°C)!”
ElseIf pCurrent > 10 Then
msg = “警告:电流超过安全阈值!”
Else
msg = “系统状态正常”
End If
RaiseEvent StatusChanged(msg) ‘ 触发事件
End Sub

‘ 二、调用

‘ 在窗体代码中:
Dim WithEvents myWire As clsWire ‘ 必须使用 WithEvents 捕获事件

Private Sub Form_Load()
Set myWire = New clsWire
myWire.Voltage = 220
myWire.Current = 5
myWire.Temperature = 75

‘ 调用方法
Debug.Print “功率:” & myWire.CalculatePower()
myWire.CheckSafety

End Sub

‘ 处理事件
Private Sub myWire_StatusChanged(ByVal Message As String)
MsgBox Message, vbInformation, “导线状态”
End Sub

晋国

晋国(前1033年至前376年):姬姓。晋国的始封君叔虞是周武王之子、周成王之弟。周成王八年(前1035年),诸侯国唐国发生叛乱,同年十月被周公旦率军平定,唐国百姓被迁至杜地 ,并于周成王十年(1033年)将叔虞分封于唐国。叔虞去世之后,其子燮继位后改国号为晋。春秋末年,赵魏韩“三家分晋”,晋国名存实亡,晋静公二年(前376年),赵魏韩瓜分了晋国公室最后的土地,晋静公被废为平民,晋国彻底灭亡。