VB6.0中類聚集關(guān)系的實(shí)現(xiàn)

字號(hào):

---- 類之間最主要的關(guān)系有兩種,它們是聚集(Aggregation)和繼承(Generelization)。聚集表示類之間的關(guān)系是整體與部分的關(guān)系,例如一個(gè)家庭有一個(gè)父親、一個(gè)母親和若干個(gè)孩子。類之間的聚集關(guān)系又稱包含關(guān)系,一個(gè)類由若干個(gè)其他類組合而成,當(dāng)該類的實(shí)例被創(chuàng)建后,組成它的各類的實(shí)例將自動(dòng)被創(chuàng)建。
    ---- 下圖描述了Family類與組成它的各類之間的關(guān)系。Family類與Father類、Mother類的關(guān)系是一對(duì)一的關(guān)系,而Family類與Child類的關(guān)系是一對(duì)多的關(guān)系。為了簡(jiǎn)化類之間的關(guān)系,我們?cè)黾恿艘粋€(gè)Children類,Children類是Child類的集合,因此Family類與Children類直接關(guān)聯(lián),形成一對(duì)一的關(guān)系。
    Family------------ > Father
    |
    -------- > Mother
    |
    -------- > Childred ------ >child
    ---- VB6.0對(duì)類聚集關(guān)系的實(shí)現(xiàn)提供了較好的支持。在下面的程序中,我們僅給出了與Falimy類、Children類、Child類的具體實(shí)現(xiàn)有關(guān)的代碼,以此為例說明類聚集關(guān)系的實(shí)現(xiàn)方法。
    ---- 程序中定義了三個(gè)類模塊:Falimy類模塊,Children類模塊,Child類模塊。在Falimy類模塊中,利用屬性過程,Mother類、Father類、Children類被定義為Family類的只讀屬性。下面是Family 類模塊中聲明部分的代碼。
    Option Explicit
    Private mFather As New Father
    Private mMother As New Mother
    Private mChildren As New Children
    Public Property Get Father() As Father
    Set Father=mMother
    End Property
    Public Property Get Mother()As Mother
    Set Mother()=mMother
    End Property
    Public Property Get Children() As Children
    Set Children= mChildren
    End Property
    ---- 下面是Children類模塊的代碼,首先在類模塊的說明部分創(chuàng)建了集合類Collection的實(shí)例mcolChildren。在定義公共方法Add時(shí),通過引用mcolChildren的Add方法將新的Child 對(duì)象添加到集合中,Children類被定義成Child類的集合。通過直接引用mcolChildren的屬性Count定義了Children類的公共屬性Count,通過直接引用mcolChildren的方法Item定義了Children類的公共方法Item。
    ---- 我們還可以根據(jù)需要實(shí)現(xiàn)其他的屬性和方法。通過創(chuàng)建Children類,與Children類有關(guān)的所有代碼都封裝起來,使得Children類可以進(jìn)一步重用,從而較好地體現(xiàn)了面向?qū)ο蟪绦蛟O(shè)計(jì)的原則。
    Option Explicit
    Private mcolChildren As New Collection
    Public Property Get Count() As Long
    Count = mcolChildren.Count
    End Property
    Public Function Add(ByVal Name As String,
    ByVal BirthDayAs Date,ByVal Sex As Boolean ) As Child
    Dim empNew As New Child
    Static intNum As Integer
    With empNew
    intNum = intNum + 1
    .Name = Name
    .BirthDay= BirthDay
    .Sex=Sex
    mcolChildren.Add empNew
    End With
    Set Add = empNew
    End Function
    Public Function Item(ByVal Index As Variant) As Child
    Set Item = mcolChildren.Item(Index)
    End Function
    ---- 下面是Child類聲明部分的代碼。
    Option Explicit
    Public Name As String
    Public BirthDay As Date
    Public Sex As Boolean
    ---- 程序中還定義了一個(gè)窗體模塊。在窗體上布置有三個(gè)文本框txtName、txtBirthDay、txtSex,一個(gè)列表框lstChildren,兩個(gè)命令按紐cmdAddChild、cmdListChild。
    ---- 在窗體模塊中首先創(chuàng)建了一個(gè)Family類的實(shí)例sbMain,Children類和Child類的實(shí)例也隨之被創(chuàng)建。在事件過程中,僅通過引用sbMain的屬性Children,我們就可以實(shí)現(xiàn)對(duì)Children類的各種操作。
    Option Explicit
    Public sbMain As New Family
    Private Sub cmdAddChild_Click()
    sbMain.Children.Add txtName.Text,
    txtSalary.Text,txtSex.Text
    txtBirthDay.Text = " "
    txtName.Text = " "
    txtSex.Text=" "
    End Sub
    Private Sub cmdListChild_Click()
    Dim emp As New Child
    Dim i As Long
    lstChild.Clear
    For i = 1 To sbMain.Children.Count
    Set emp = sbMain.Children.Item(i)
    lstChild.AddItem emp.Name & ", "
    & emp.BirthDay&", "emp.Sex
    Next
    End Sub