大雀软件园

首页 软件下载 安卓市场 苹果市场 电脑游戏 安卓游戏 文章资讯 驱动下载
技术开发 网页设计 图形图象 数据库 网络媒体 网络安全 站长CLUB 操作系统 媒体动画 安卓相关
当前位置: 首页 -> 技术开发 -> NET专区 -> VB.Net中文教程(13) Whole-Part关系

VB.Net中文教程(13) Whole-Part关系

时间: 2021-07-31 作者:daque

中心: whole-part联系?????????? 实质 ??????????v 1. 东西whole-part联系v 2. 拉拢/局部联系v 3. 包括者/实质联系v 4. 汇合/分子联系1. 东西whole-part联系 类型接受(class inheritance)和东西拉拢(object composition)是软硬件再运用(reuse)的两大宝贝。类型接受即是创造父、子类型之联系﹔比方﹐「弟子」可分为「大弟子」、「中弟子」和「小弟子」三类型﹐其接受联系图标如次﹕ 图1、 以uml表白类型接受 东西拉拢的手段是﹕创作「复合东西」(composite object)﹔比方﹐病院内含医生和看护等﹐其拉拢联系图标如次﹕ 图2、 以uml表白东西拉拢接受与拉拢两大宝贝能共同运用﹐以构造宏大的软硬件体例。比方﹐公共汽车分为客车、货车、卧车等子类型﹐并且公共汽车内含引擎、车体、轮带等零件﹐则此公共汽车体例图标如次图3和图4﹕ 图3、 公共汽车的类型接受体制 图4、 公共汽车的东西拉拢联系本节里﹐将进一步领会与证明东西拉拢本领。尤顿(yourdon) 觉得﹐罕见拉拢联系有三﹕ 1) 拉拢╱局部(assembly-parts)联系。 2) 包括╱实质(container-contents)联系。 3) 汇合╱分子(collection-members)联系。2. 拉拢/局部联系 拉拢/局部联系﹐常称为apo(a part of)联系﹔比方﹐公共汽车是「拉拢」﹐其内含各零件是「局部」。门是屋子的一局部﹐以是屋子是「拉拢」﹐门是「局部」﹔其余﹐窗子也是屋子的「局部」。这屋子与窗门之联系﹐图标如次﹕ 图5、 屋子的东西拉拢联系以vb表白如次﹕'ex01.basimports system.componentmodelimports system.drawingimports 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 subend 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 subend class以此步调输入如次﹕ door: 50 win: 100house 之东西出生后﹐登时出生内含之door东西和window东西。比方﹐颁布训令── dim h as new house()此时﹐h 东西出生了﹐其内含之dr 和win东西亦出生了。此h 通称为「拉拢东西」(composite object)﹐而dr 和win 则称为「局部东西」(component object)。这种联系具备一项特性﹕拉拢东西与局部东西的寿命该当是普遍的。 在论理(logical)意旨上,这house 构造中﹐门和窗跟着屋子而具备「存亡与共」之亲蜜联系,也即是寿命普遍。在计划机实业(physical)表白时,house 之东西并不「真实」包括door及window之东西﹐不过运用两个参考指向它们。以是上海图书馆也可设想如次:上述两种实业构造皆表白了「拉拢/局部」联系。请再看个例子:'ex02.basimports system.componentmodelimports system.drawingimports 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 subend 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 subend class此步调输入﹕ alvin, 35 david, 25 0 1 「拉拢」东西之建构者new()步调出生「局部」东西。此a 、b 两东西之实质为﹕在isequal()步调里,两个东西拿其p_name 值来比拟。比方﹐a.p_name 值是"alvin" ﹐而b.p_name 值是"david" ﹐以是a.isequal(b)表白式之值为0 (false)。3. 包括者/实质联系 上节的house 构造中﹐门和窗跟着屋子而具备「存亡与共」之亲蜜联系。但是﹐凡是生存中﹐罕见一致但并不如许亲蜜的景象。比方﹐遨游员坐于铁鸟驾驶仓内开铁鸟﹔司机在公共汽车内驾驶公共汽车﹔宾客乘座于巴士内之类。司机不是公共汽车的零件﹐宾客亦非巴士之组件﹐以是公共汽车与司机之间并非「拉拢/局部」联系﹔但是﹐公共汽车简直包括着司机﹐因之称为「包括者/实质」(container-contents)联系。 司机和公共汽车为独力之东西﹐不像引擎从来包括于公共汽车内﹔于驾驶公共汽车时﹐司机才被包括于公共汽车内。明显地﹐司机与公共汽车之寿命不一律长。「包括者/实质」联系是一种特出的拉拢构造﹐其图标本领与「拉拢/局部」联系沟通。比方﹐此图表白了﹕ ◎ 公共汽车与引擎之间为「拉拢/局部」联系。 ◎ 公共汽车与司机之间为「包括者/实质」联系。以vb表白如次﹕'ex03.basimports system.componentmodelimports system.drawingimports 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 subend classclass 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 subend 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 subend class此步调输入﹕ model: honda driver: wang model: honda model" kaocar之东西出生后﹐也出生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.basimports system.componentmodelimports system.drawingimports 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 subend 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 subend class此步调输入﹕david, 32, sp: hellenhellen, 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.basimports system.componentmodelimports system.drawingimports system.winformsimports 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 subend classclass 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 subend 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 subend class此步调输入﹕club: sogo has member:alvin, 32judy, 28c_name 指向strclass之东西﹐这东西内含俱乐部之称呼。pa指向arraylist之东西﹐这东西可包括很多会员(person)东西。join()步调将person之东西惠存pa所指的arraylist东西中。 club之东西含arraylist之东西﹐此汇合东西(collections)含有person之东西﹐表白了「汇合/分子」联系。比方﹐x 东西内含a 和b 东西。 此图表白﹕"sogo"俱乐部公有"alvin" 和"judy"两个会员﹐亦即x 是「汇合」﹐而a 和b 是「分子」(member)。 犯得着提防﹕这软硬件是运用已有类型──strclass及integer拉拢成运用类型──person。再运用person类型及arraylist 类型拉拢成更搀杂之运用类型──club。将来﹐可运用club及其它类型修建更大的运用类型﹐依该类推﹐便能创作宏大又真实的软硬件了。比方:'ex06.basimports system.componentmodelimports system.drawingimports system.winformsimports 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 subend classclass 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 subend 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 subend class此步调输入:club: sogo has member: alvin, 32 judy, 28club: gold has member: judy, 28 bob, 38 拉拢东西x 含"sogo"及"gold"两俱乐部﹐个中"gold"俱乐部具有两个会员──"alvin" 及"judy"﹐而"sogo"俱乐部具有两位会员──"judy"及"bob" 。x(0)代办"sogo"俱乐部﹐s(1)代办"gold"俱乐部﹐以是训令── s(0).join( a ) 表白a 介入"gold"俱乐部﹐变成其会员。n

热门阅览

最新排行

Copyright © 2019-2021 大雀软件园(www.daque.cn) All Rights Reserved.