|
String) name = na End Sub Public Sub Show() MessageBox.Show("Driver: " + name) End Sub End Class
Class Car Class Engine Public model As String Public Sub New(ByVal mdl As String) model = mdl End Sub End Class Private e As Engine Private dr As Driver Public Sub New() e = New Engine("Honda") End Sub Public Sub assignTo(ByVal d As Driver) dr = d End Sub Public Sub Show() MessageBox.Show("Engine: " + e.model) dr.Show() 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 civic As New Car() Dim d1 As New Driver("Wang") Dim d2 As New Driver("Kao") civic.assignTo(d1) civic.Show() civic.assignTo(d2) civic.Show() End Sub End Class
此程序输出﹕ Model: Honda Driver: Wang Model: Honda Model" Kao
Car之对象诞生后﹐也诞生Engine之对象e ﹔同时立即指定司机﹐如下指令﹕ Dim civic As New Car() Dim d1 As New Driver("Wang") ..... civic.assignTo(d1) .....
日常生活中的常见情况﹕汽车对象诞生时﹐不须立即指定司机对象。例如﹐汽车出厂时或闲置时并无司机﹐且汽车经常更换司机。此情形下﹐应先诞生civic对象和d1对象,如下:
此时,未立即指定司机﹔而必要时才以assignTo()程序指定司机。例如,将d1指定给civic对象﹐就令civic内之参考变量dr指向d1 对象,如下:
上述程序里,d1、d2及civic 对象之间﹐谁先诞生并无关紧要﹐各独立存在。指令──civic.assignTo(d1) 将d1司机指定给civic 对象﹔另一指令──civic.assignTo(d2)表示﹕改由d2担任civic 之司机。 此Car 类别以参考变量e来指向Engine之对象。现在﹐兹拿上节的Person类别做为例子﹐如果某人(Person 之对象)结婚了﹐就有配偶﹔反之尚未结婚时﹐则无配偶。此时﹐应将Person类别之定义修改如下﹐以表达这种「配偶」关系﹕
''''ex04.bas Imports System.ComponentModel Imports System.Drawing Imports System.WinForms ''''---------------------------------------------------- Class Person Private p_name As String Private p_age As Integer Private p_spouse As Person Public Sub New(ByVal na As String, ByVal a As Integer) p_name = na p_age = a End Sub Public Sub spouse(ByVal sp As Person) p_spouse = sp sp.p_spouse = Me End Sub Public Sub Show() Messagebox.Show(Me.p_name + ", " + str(Me.p_age) + ", sp: " + Me.p_spouse.p_name) 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 x As New Person("David", 32) Dim y As New Person("Hellen", 24) x.spouse(y) x.Show() y.Show() End Sub End Class
此程序输出﹕ David, 32, sp: Hellen Hellen, 24, sp: David
资料成员p_spouse指向配偶﹐而配偶亦为Person之对象。所以p_spouse之型态应为Person。一个人刚诞生时并无配偶﹐到结婚时才有配偶﹐所以藉spouce()来建立配偶关系。对象x 与y 结婚之后﹐互为对方之配偶。所以x.p_spouse指向y ﹐而y.p_spouse则指向x 。此时﹐x 和y 之内容如下﹕
于是这Person类别表达了「婚姻」关系。
4. 集合/成员关系
集合意谓着「团体」(Group) ﹐由其成员(Member)组成的群体。例如﹐学校里的社团内有团员﹔公司的销售部含有推销人员。这团体并不像汽车实际包含着司机﹐而只是其成员之集合而已。这情形﹐统称为「集合/成员」(Collection-members)关系。 有趣的是﹕在企业活动中﹐人们规划的方案﹐含许多小方案﹔则大方案是由小方案所组成。例如﹐东北亚旅行团的行程表包括在日本的观光行程、在韩国的观光行程和在香港的观光行程。这总行程表图标如下﹕
总行程是次行程(或称段行程)之集合﹐这是「集合/成员」关系。
此外﹐棒球队是由经理、教练和球员组成﹕订单中含若干产品项目﹐皆为集合/成员关系。实际写程序时﹐不需明确划分「包含者/内容」和「集合/成员」两种关系。其原因是﹕集合与成员之间亦可互为独立﹐不具「生死与共」之亲蜜关系﹔例如﹐「香港观光行程」是独立存在的﹐它既可含于东北亚总行程中﹐又可含于东南亚旅行团的总行程中。因之﹐「集合/成员」关系是一种特殊的「组合」(Composition) 结构。 兹拿上节Person类别做为例子﹐如果Person之对象会加入Club(俱乐部)成为俱乐部的会员﹐则Club与Person之关系为「集合/成员」关系。兹定义Club类别如下﹕
''''ex05.bas Imports System.ComponentModel Imports System.Drawing Imports System.WinForms Imports System.Collections ''''---------------------------------------------------- Class Person Private p_name As String Private p_age As Integer Public Sub New(ByVal na As String, ByVal a As Integer) p_name = na p_age = a End Sub Public Sub display() Messagebox.Show(Me.p_name + ", " + str(Me.p_age)) End Sub End Class
Class Club Private c_name As String Private pa As ArrayList Public Sub New(ByVal na As String) c_name = na pa = New ArrayList() End Sub Public Sub join(ByVal p As Person) pa.Add(p) End Sub Public Sub display() Messagebox.Show("Club: " + Me.c_name + " has member:") Dim p As Person For Each p In pa p.display() Next End Sub En 上一页 [1] [2] [3] 下一页 [Delphi程序]The Delphi Object Model (PART III) [Delphi程序]The Delphi Object Model (PART II) [Delphi程序]The Delphi Object Model (PART I) [Delphi程序]Delphi对象模型(Part III) [Delphi程序]Delphi对象模型(Part II) [Delphi程序]Delphi对象模型(Part I) [Delphi程序]Delphi对象模型(Part V) [Delphi程序]Delphi对象模型(Part IV) [Delphi程序]Delphi对象模型(Part VI) [Delphi程序]防止全局hook入侵Delphi版,2000以上系统适用(pa…
|