|
size = s
End Sub
End Class
Private dr As Door
Private win As Window
Public Sub New()
dr = New Door(50)
win = New Window(100)
End Sub
Public Sub Show()
Messagebox.Show("Door: " + str(dr.size) +
" Win: " + str(win.size))
End Sub
End Class
'-------
Public Class Form1
Inherits System.WinForms.Form
Public Sub New()
MyBase.New()
Form1 = Me
'This call is required by the Win Form Designer.
InitializeComponent()
'TODO: Add any initialization after the InitializeComponent()
call
End Sub
'Form overrides dispose to clean up the component list.
Public Overrides Sub Dispose()
MyBase.Dispose()
components.Dispose()
End Sub
#Region " Windows Form Designer generated code "
......
#End Region
Protected Sub Form1_Click(ByVal sender As Object, ByVal
e As System.EventArgs)
Dim h As New House()
h.Show()
End Sub
End Class
以此程序输出如下﹕
Door:
50 Win: 100
House 之对象诞生后﹐立即诞生内含之Door对象和Window对象。例如﹐宣告指令──
Dim h As New House()
此时﹐h 对象诞生了﹐其内含之dr 和win对象亦诞生了。
此h 通称为「组合对象」(Composite object)﹐而dr 和win 则称为「部分对象」(Component object)。这种关系具有一项特色﹕组合对象与部分对象的寿命应该是一致的。
在逻辑(Logical)意义上,这House 结构中﹐门和窗随着房子而具有「生死与共」之亲蜜关系,也就是寿命一致。在计算机实体(Physical)表达时,House
之对象并不「真正」包含Door及Window之对象﹐只是利用两个参考指向它们。所以上图也可想象如下:
|