国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

VB.Net中文教程(13) Whole-Part關(guān)系

2024-07-10 13:02:42
字體:
供稿:網(wǎng)友
主題: whole-part關(guān)系


?????????? 內(nèi)容 ??????????
v 1. 對(duì)象whole-part關(guān)系
v 2. 組合/部分關(guān)系
v 3. 包含者/內(nèi)容關(guān)系
v 4. 集合/成員關(guān)系





1. 對(duì)象whole-part關(guān)系

類別繼承(class inheritance)和對(duì)象組合(object composition)是軟件再使用(reuse)的兩大法寶。類別繼承就是建立父、子類別之關(guān)系﹔例如﹐「學(xué)生」可分為「大學(xué)生」、「中學(xué)生」和「小學(xué)生」三類別﹐其繼承關(guān)系圖標(biāo)如下﹕

圖1、 以u(píng)ml表達(dá)類別繼承

對(duì)象組合的目的是﹕創(chuàng)造「復(fù)合對(duì)象」(composite object)﹔例如﹐醫(yī)院內(nèi)含醫(yī)師和護(hù)士等﹐其組合關(guān)系圖標(biāo)如下﹕


圖2、 以u(píng)ml表達(dá)對(duì)象組合

繼承與組合兩大法寶能聯(lián)合使用﹐以組織龐大的軟件系統(tǒng)。例如﹐汽車分為客車、卡車、轎車等子類別﹐而且汽車內(nèi)含引擎、車體、輪胎等零件﹐則此汽車系統(tǒng)圖標(biāo)如下圖3和圖4﹕



圖3、 汽車的類別繼承體系


圖4、 汽車的對(duì)象組合關(guān)系

本節(jié)里﹐將進(jìn)一步分析與說明對(duì)象組合方法。尤頓(yourdon) 認(rèn)為﹐常見組合關(guān)系有三﹕
1) 組合╱部分(assembly-parts)關(guān)系。
2) 包含╱內(nèi)容(container-contents)關(guān)系。
3) 集合╱成員(collection-members)關(guān)系。





2. 組合/部分關(guān)系

組合/部分關(guān)系﹐常稱為apo(a part of)關(guān)系﹔例如﹐汽車是「組合」﹐其內(nèi)含各零件是「部分」。門是房子的一部分﹐所以房子是「組合」﹐門是「部分」﹔此外﹐窗子也是房子的「部分」。這房子與門窗之關(guān)系﹐圖標(biāo)如下﹕


圖5、 房子的對(duì)象組合關(guān)系

以vb表達(dá)如下﹕

'ex01.bas
imports system.componentmodel
imports system.drawing
imports system.winforms
'---------------------------------------------------------------------------------
class house
class door
public size as double
public sub new(byval s as double)
size = s
end sub
end class

class window
public size as double
public sub new(byval s as double)
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 之對(duì)象誕生后﹐立即誕生內(nèi)含之door對(duì)象和window對(duì)象。例如﹐宣告指令──
dim h as new house()

此時(shí)﹐h 對(duì)象誕生了﹐其內(nèi)含之dr 和win對(duì)象亦誕生了。



此h 通稱為「組合對(duì)象」(composite object)﹐而dr 和win 則稱為「部分對(duì)象」(component object)。這種關(guān)系具有一項(xiàng)特色﹕組合對(duì)象與部分對(duì)象的壽命應(yīng)該是一致的。
在邏輯(logical)意義上,這house 結(jié)構(gòu)中﹐門和窗隨著房子而具有「生死與共」之親蜜關(guān)系,也就是壽命一致。在計(jì)算機(jī)實(shí)體(physical)表達(dá)時(shí),house 之對(duì)象并不「真正」包含door及window之對(duì)象﹐只是利用兩個(gè)參考指向它們。所以上圖也可想象如下:


上述兩種實(shí)體結(jié)構(gòu)皆表達(dá)了「組合/部分」關(guān)系。請(qǐng)?jiān)倏磦€(gè)例子:

'ex02.bas
imports system.componentmodel
imports system.drawing
imports system.winforms
'----------------------------------------------------
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 function isequal(byval obj as person) as integer
dim k as integer = 0
if me.p_name = obj.p_name then
k = 1
end if
isequal = k
end function
public sub show()
messagebox.show(me.p_name + ", " + str(me.p_age))
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 a as new person("alvin", 35)
dim b as new person("david", 25)
a.show()
b.show()
messagebox.show(str(a.isequal(b)))
messagebox.show(str(b.isequal(new person("david", 32))))
end sub
end class

此程序輸出﹕
alvin, 35
david, 25
0
1

「組合」對(duì)象之建構(gòu)者new()程序誕生「部分」對(duì)象。此a 、b 兩對(duì)象之內(nèi)容為﹕






在isequal()程序里,兩個(gè)對(duì)象拿其p_name 值來比較。例如﹐a.p_name 值是"alvin" ﹐而b.p_name 值是"david" ﹐所以a.isequal(b)表達(dá)式之值為0 (false)。







3. 包含者/內(nèi)容關(guān)系

上節(jié)的house 結(jié)構(gòu)中﹐門和窗隨著房子而具有「生死與共」之親蜜關(guān)系。然而﹐日常生活中﹐常見類似但并不如此親蜜的情形。例如﹐飛行員坐于飛機(jī)駕駛倉內(nèi)開飛機(jī)﹔司機(jī)在汽車內(nèi)駕駛汽車﹔客人乘座于巴士內(nèi)等等。司機(jī)不是汽車的零件﹐客人亦非巴士之組件﹐所以汽車與司機(jī)之間并非「組合/部分」關(guān)系﹔然而﹐汽車的確包含著司機(jī)﹐因之稱為「包含者/內(nèi)容」(container-contents)關(guān)系。
司機(jī)和汽車為獨(dú)立之對(duì)象﹐不像引擎一直包含于汽車內(nèi)﹔于駕駛汽車時(shí)﹐司機(jī)才被包含于汽車內(nèi)。顯然地﹐司機(jī)與汽車之壽命不一樣長。「包含者/內(nèi)容」關(guān)系是一種特殊的組合結(jié)構(gòu)﹐其圖標(biāo)方法與「組合/部分」關(guān)系相同。例如﹐


此圖表達(dá)了﹕
◎ 汽車與引擎之間為「組合/部分」關(guān)系。
◎ 汽車與司機(jī)之間為「包含者/內(nèi)容」關(guān)系。

以vb表達(dá)如下﹕

'ex03.bas
imports system.componentmodel
imports system.drawing
imports system.winforms
'----------------------------------------------------
class driver
private name as string
public sub new(byval na as 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之對(duì)象誕生后﹐也誕生engine之對(duì)象e ﹔同時(shí)立即指定司機(jī)﹐如下指令﹕

dim civic as new car()
dim d1 as new driver("wang")
.....
civic.assignto(d1)
.....

日常生活中的常見情況﹕汽車對(duì)象誕生時(shí)﹐不須立即指定司機(jī)對(duì)象。例如﹐汽車出廠時(shí)或閑置時(shí)并無司機(jī)﹐且汽車經(jīng)常更換司機(jī)。此情形下﹐應(yīng)先誕生civic對(duì)象和d1對(duì)象,如下:



此時(shí),未立即指定司機(jī)﹔而必要時(shí)才以assignto()程序指定司機(jī)。例如,將d1指定給civic對(duì)象﹐就令civic內(nèi)之參考變量dr指向d1 對(duì)象,如下:



上述程序里,d1、d2及civic 對(duì)象之間﹐誰先誕生并無關(guān)緊要﹐各獨(dú)立存在。指令──civic.assignto(d1) 將d1司機(jī)指定給civic 對(duì)象﹔另一指令──civic.assignto(d2)表示﹕改由d2擔(dān)任civic 之司機(jī)。
此car 類別以參考變量e來指向engine之對(duì)象。現(xiàn)在﹐茲拿上節(jié)的person類別做為例子﹐如果某人(person 之對(duì)象)結(jié)婚了﹐就有配偶﹔反之尚未結(jié)婚時(shí)﹐則無配偶。此時(shí)﹐應(yīng)將person類別之定義修改如下﹐以表達(dá)這種「配偶」關(guān)系﹕

'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之對(duì)象。所以p_spouse之型態(tài)應(yīng)為person。一個(gè)人剛誕生時(shí)并無配偶﹐到結(jié)婚時(shí)才有配偶﹐所以藉spouce()來建立配偶關(guān)系。對(duì)象x 與y 結(jié)婚之后﹐互為對(duì)方之配偶。所以x.p_spouse指向y ﹐而y.p_spouse則指向x 。此時(shí)﹐x 和y 之內(nèi)容如下﹕



于是這person類別表達(dá)了「婚姻」關(guān)系。






4. 集合/成員關(guān)系

集合意謂著「團(tuán)體」(group) ﹐由其成員(member)組成的群體。例如﹐學(xué)校里的社團(tuán)內(nèi)有團(tuán)員﹔公司的銷售部含有推銷人員。這團(tuán)體并不像汽車實(shí)際包含著司機(jī)﹐而只是其成員之集合而已。這情形﹐統(tǒng)稱為「集合/成員」(collection-members)關(guān)系。
有趣的是﹕在企業(yè)活動(dòng)中﹐人們規(guī)劃的方案﹐含許多小方案﹔則大方案是由小方案所組成。例如﹐東北亞旅行團(tuán)的行程表包括在日本的觀光行程、在韓國的觀光行程和在香港的觀光行程。這總行程表圖標(biāo)如下﹕



總行程是次行程(或稱段行程)之集合﹐這是「集合/成員」關(guān)系。



此外﹐棒球隊(duì)是由經(jīng)理、教練和球員組成﹕訂單中含若干產(chǎn)品項(xiàng)目﹐皆為集合/成員關(guān)系。實(shí)際寫程序時(shí)﹐不需明確劃分「包含者/內(nèi)容」和「集合/成員」兩種關(guān)系。其原因是﹕集合與成員之間亦可互為獨(dú)立﹐不具「生死與共」之親蜜關(guān)系﹔例如﹐「香港觀光行程」是獨(dú)立存在的﹐它既可含于東北亞總行程中﹐又可含于東南亞旅行團(tuán)的總行程中。因之﹐「集合/成員」關(guān)系是一種特殊的「組合」(composition) 結(jié)構(gòu)。
茲拿上節(jié)person類別做為例子﹐如果person之對(duì)象會(huì)加入club(俱樂部)成為俱樂部的會(huì)員﹐則club與person之關(guān)系為「集合/成員」關(guān)系。茲定義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
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 club("sogo")
dim a as new person("alvin", 32)
dim b as new person("judy", 28)
x.join(a)
x.join(b)
x.display()
end sub
end class


此程序輸出﹕
club: sogo has member:
alvin, 32
judy, 28

c_name 指向strclass之對(duì)象﹐這對(duì)象內(nèi)含俱樂部之名稱。pa指向arraylist之對(duì)象﹐這對(duì)象可包含許多會(huì)員(person)對(duì)象。join()程序?qū)erson之對(duì)象存入pa所指的arraylist對(duì)象中。
club之對(duì)象含arraylist之對(duì)象﹐此集合對(duì)象(collections)含有person之對(duì)象﹐表達(dá)了「集合/成員」關(guān)系。例如﹐x 對(duì)象內(nèi)含a 和b 對(duì)象。



此圖表示﹕"sogo"俱樂部共有"alvin" 和"judy"兩個(gè)會(huì)員﹐亦即x 是「集合」﹐而a 和b 是「成員」(member)。
值得注意﹕這軟件是利用已有類別──strclass及integer組合成應(yīng)用類別──person。再利用person類別及arraylist 類別組合成更復(fù)雜之應(yīng)用類別──club。未來﹐可利用club及其它類別構(gòu)筑更大的應(yīng)用類別﹐依此類推﹐便能創(chuàng)造龐大又可靠的軟件了。例如:

'ex06.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
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(2) as club
x(0) = new club("sogo")
x(1) = new club("gold")
dim a as new person("alvin", 32)
dim b as new person("judy", 28)
dim c as new person("bob", 38)
x(0).join(a)
x(0).join(b)
x(1).join(b)
x(1).join(c)
x(0).display()
x(1).display()
end sub
end class

此程序輸出:

club: sogo has member:
alvin, 32
judy, 28
club: gold has member:
judy, 28
bob, 38

組合對(duì)象x 含"sogo"及"gold"兩俱樂部﹐其中"gold"俱樂部擁有兩個(gè)會(huì)員──"alvin" 及"judy"﹐而"sogo"俱樂部擁有兩位會(huì)員──"judy"及"bob" 。x(0)代表"sogo"俱樂部﹐s(1)代表"gold"俱樂部﹐所以指令── s(0).join( a ) 表示a 加入"gold"俱樂部﹐成為其會(huì)員。n

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 商河县| 南城县| 盘锦市| 商丘市| 德安县| 耒阳市| 呼图壁县| 酒泉市| 高密市| 连平县| 松江区| 双流县| 白银市| 尉犁县| 汉阴县| 古蔺县| 四川省| 奈曼旗| 略阳县| 屯昌县| 乡宁县| 吴忠市| 恩平市| 朝阳县| 鄄城县| 徐闻县| 喜德县| 惠水县| 台北市| 金秀| 深州市| 镶黄旗| 九台市| 临洮县| 五华县| 呼图壁县| 铁力市| 和静县| 靖远县| 澄江县| 皮山县|