Excel VBA如何创建嵌套其他类模块的Room类并实现参数传递
Excel VBA 嵌套类模块(ConditionRoom)实现步骤
第一步:创建基础子属性类
先完成Window、Wall、Roof三个独立类模块的定义,所有类模块命名需要和下方类名完全一致。
1.1 Window类模块
' Window类模块代码 Private p_GlassType As String ' 玻璃类型:单层/双层 Private p_FrameMaterial As String ' 窗框材质:木质/铝合金 ' 玻璃类型属性 Public Property Get GlassType() As String GlassType = p_GlassType End Property Public Property Let GlassType(Value As String) p_GlassType = Value End Property ' 窗框材质属性 Public Property Get FrameMaterial() As String FrameMaterial = p_FrameMaterial End Property Public Property Let FrameMaterial(Value As String) p_FrameMaterial = Value End Property
1.2 Wall类模块
' Wall类模块代码 Private p_Material As String ' 墙体材质 Private p_Thickness As Double ' 墙体厚度(单位:mm) Private p_InsulationLevel As String ' 保温等级 Public Property Get Material() As String Material = p_Material End Property Public Property Let Material(Value As String) p_Material = Value End Property Public Property Get Thickness() As Double Thickness = p_Thickness End Property Public Property Let Thickness(Value As Double) p_Thickness = Value End Property Public Property Get InsulationLevel() As String InsulationLevel = p_InsulationLevel End Property Public Property Let InsulationLevel(Value As String) p_InsulationLevel = Value End Property
1.3 Roof类模块
' Roof类模块代码 Private p_Slope As Double ' 屋面坡度(单位:°) Private p_Material As String ' 屋面材质 Private p_HasSkylight As Boolean ' 是否带天窗 Public Property Get Slope() As Double Slope = p_Slope End Property Public Property Let Slope(Value As Double) p_Slope = Value End Property Public Property Get Material() As String Material = p_Material End Property Public Property Let Material(Value As String) p_Material = Value End Property Public Property Get HasSkylight() As Boolean HasSkylight = p_HasSkylight End Property Public Property Let HasSkylight(Value As Boolean) p_HasSkylight = Value End Property
第二步:创建ConditionRoom主类模块
插入新的类模块,命名为ConditionRoom,代码如下:
' ConditionRoom类模块代码 Private p_Width As Double ' 宽度(单位:m) Private p_Length As Double ' 长度(单位:m) Private p_Height As Double ' 高度(单位:m) ' 子属性对象声明 Private p_Window As Window Private p_Wall As Wall Private p_Roof As Roof ' 类初始化:自动实例化子对象,避免调用时报对象未定义错误 Private Sub Class_Initialize() Set p_Window = New Window Set p_Wall = New Wall Set p_Roof = New Roof End Sub ' 类销毁:释放对象内存 Private Sub Class_Terminate() Set p_Window = Nothing Set p_Wall = Nothing Set p_Roof = Nothing End Sub ' 宽度属性 Public Property Get Width() As Double Width = p_Width End Property Public Property Let Width(Value As Double) If Value > 0 Then p_Width = Value ' 可自定义合法性校验规则 End Property ' 长度属性 Public Property Get Length() As Double Length = p_Length End Property Public Property Let Length(Value As Double) If Value > 0 Then p_Length = Value End Property ' 高度属性 Public Property Get Height() As Double Height = p_Height End Property Public Property Let Height(Value As Double) If Value > 0 Then p_Height = Value End Property ' 体积属性:只读,自动关联长宽高计算 Public Property Get Volume() As Double Volume = p_Width * p_Length * p_Height End Property ' Window子属性:返回对象实例可直接修改内部属性 Public Property Get Window() As Window Set Window = p_Window End Property ' Wall子属性 Public Property Get Wall() As Wall Set Wall = p_Wall End Property ' Roof子属性 Public Property Get Roof() As Roof Set Roof = p_Roof End Property
第三步:调用测试示例
在普通模块中运行以下代码验证功能:
Sub TestConditionRoom() ' 实例化主类 Dim room As New ConditionRoom ' 基础属性赋值 room.Width = 3.6 room.Length = 4.5 room.Height = 2.8 ' 子属性赋值 room.Window.GlassType = "双层" room.Window.FrameMaterial = "铝合金" room.Wall.Material = "加气混凝土" room.Wall.Thickness = 200 room.Roof.Material = "沥青瓦" room.Roof.HasSkylight = True ' 读取属性输出测试 Debug.Print "房间体积:" & room.Volume & " m³" Debug.Print "窗户玻璃类型:" & room.Window.GlassType Debug.Print "墙体材质:" & room.Wall.Material Debug.Print "屋面是否带天窗:" & IIf(room.Roof.HasSkylight, "是", "否") End Sub
注意事项
- 所有类模块的名称必须和代码中引用的类名完全一致,不能加空格或自定义后缀
- 体积设置为只读属性避免手动修改,保证和长宽高数据的一致性
- 类初始化方法中已经提前实例化三个子对象,调用方无需手动New子对象,简化使用逻辑
内容的提问来源于stack exchange,提问作者iatnart
相关产品推荐
相关产品推荐

