大雀软件园

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

VB.Net中文教程(6) 母子对象关系

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

1. 特出whole-part联系 ---- 母子东西联系 大师仍旧熟习爷儿俩类型联系﹐也即是「接受」联系了。于此证明另一种罕见联系── 母子东西。普遍称之为「拉拢/局部」联系。凡是生存中﹐到处看来到这种母子东西。比方﹐客堂内有沙发、台子、椅子之类。客堂是母东西﹐沙发、台子、椅子是子东西。再如计划机屏幕上的窗口内有按钮、采用表、对话盒之类。窗口是母东西﹐按钮、采用表是子东西。于此将证明怎样创造母子东西联系。有了联系﹐母子就能彼此勾通了。母子东西之间﹐怎样勾通呢﹖也即是说﹐母东西怎样呼唤子东西的步调呢﹖反过来﹐子东西怎样呼唤母东西的步调呢﹖欲呼唤对方的步调﹐必先与对方创造联系才行。因之﹐怎样创造母子东西联系﹐是顶要害之课题﹗ 请先看个例子﹐有两个类型──room和desk。若room代办屋子﹐desk代办屋子内的台子﹐则它们会出生母子东西﹕常常﹐您会依屋子的巨细来确定台子的巨细。因之﹐desk东西应跟room东西勾通﹐博得屋子的巨细﹐才确定本人的巨细。若有个room之参考﹐则desk东西就能跟room东西勾通了。所以﹐可安排下述vb步调:'ex01.basimports system.componentmodelimports system.drawingimports system.winforms'----------------------------------------------------class room protected rsize as double shared motherobject as room public sub new() motherobject = me end sub shared function getmother() as room getmother = motherobject end function public function getsize() as double getsize = rsize end functionend classclass desk protected dsize as double public sub new() dsize = room.getmother().getsize() * 0.18 end sub public function getsize() as double getsize = dsize end functionend class'----------------------------------------------------class myroom inherits room private rd as desk public sub new() rsize = 100 rd = new desk() end sub public sub show() messagebox.show("room size: " + str(rsize)) messagebox.show("desk size: " + str(rd.getsize())) 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 r as new myroom() r.show() end subend class此步调输入: room size: 100 desk size: 18desk类型内之训令── room.getmother().getsize()即是desk东西与room东西之勾通了。myroom类型有个rd参考﹐指向子东西﹐如次﹕母东西经过rd参考来跟子东西勾通。反过来﹐子东西经过room.getmother()参考跟母东西勾通。2. 母子东西谁先出生﹖ 在vb里﹐有功夫子东西比母东西早出生实行。就像创造屋子﹐建到中途﹐改而创造台子﹐台子建好了﹐再连接把屋子建实行。因为这不太符合人们的凡是生存风气﹐令人感触迷惑﹗在计划机软硬件上﹐也有一致的辩论。比方﹐在windows 下﹐母窗口建好了﹐才创造窗内的按钮、采用表等子东西。但vb偶尔并非如许﹗因之﹐怎样弥合这辩论﹐是个要害的话题。兹拿上末节的例子来证明吧﹗该例子中﹐其出生东西之进程为﹕step 1: 先出生母东西。 step 2: 接着创造子东西。这进程符合人们的风气﹐是优美的。其因为是﹕myroom类型在其建构者new()步调里呼唤desk的new()出生desk子东西。假如您写下述步调﹐就出题目了﹗'ex02.bas'some error here!imports system.componentmodelimports system.drawingimports system.winforms'----------------------------------------------------class room protected rsize as double shared motherobject as room public sub new() motherobject = me end sub shared function getmother() as room getmother = motherobject end function public function getsize() as double getsize = rsize end functionend classclass desk protected dsize as double public sub new() dsize = room.getmother().getsize() * 0.18 end sub public function getsize() as double getsize = dsize end functionend class'----------------------------------------------------class myroom inherits room private rd as new desk() public sub new() rsize = 100 end sub public sub show() messagebox.show("room size: " + str(rsize)) messagebox.show("desk size: " + str(rd.getsize())) 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 r as new myroom() r.show() end subend class其东西之创造进程为﹕step 1: 训令── dim r as new myroom()呼唤myroom()建构者new()。它再呼唤room()建构者new()将room局部建好。step 2: 实行训令── private rd as new desk() 呼唤desk建构者new()﹐建好desk子东西。此时会实行训令── dsize = room.getmother().getsize() * 0.18step 3: myroom建构者new() 连接将母东西建实行。 此时会实行训令── rsize=100 个中,getsize()步调会掏出rsize 值﹐但其时还未实行rsize=100 训令﹐那来的rsize 值呢﹖以是出题目了。此步调大概输入如次缺点截止﹕ room size: 100 desk size: 0怎样处置上述题目呢﹖罕见本领是﹕ 把会出题目的训令﹐从建构者步调中提出来﹐放到另一步调里。比方下述步调:'ex03.basimports system.componentmodelimports system.drawingimports system.winforms'----------------------------------------------------class room protected rsize as double shared motherobject as room shared function getmother() as room getmother = motherobject end function public overridable sub create() motherobject = me end sub public function getsize() as double getsize = rsize end functionend classclass desk protected dsize as double public sub create() dsize = room.getmother().getsize() * 0.18 end sub public function getsize() as double getsize = dsize end functionend class'----------------------------------------------------class myroom inherits room private rd as new desk() public sub new() me.create() end sub public overrides sub create() mybase.create() rsize = 100 rd.create() end sub public sub show() messagebox.show("room size: " + str(rsize)) messagebox.show("desk size: " + str(rd.getsize())) 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 r as new myroom() r.show() end subend class此步调输入: room size: 100 desk size: 18像训令── dsize = room.getmother().getsize() * 0.18﹐已挪到新设的create()步调里。待母东西实足建好了﹐才会呼唤这create()步调﹐getsize() 就能博得精确值了。myroom的new()呼唤create()步调时﹐母子东西皆已创造实行了。create()里面依人们的风气来设定东西之值,比方创造母子东西之联系。如许就不会出题目了。 new()与create()辨别之后,myroom类型里的训令:class myroom inherits room private rd as new desk() public sub new() me.create() end sub........也能写为:class myroom inherits room private rd as desk public sub new() rd = new desk() me.create() end sub........只有保证desk类型的训令── dsize = room.getmother().getsize() * 0.18是在myroom类型的训令── rsize = 100之后实行即可了。 上述的子东西是透过shared 步调来博得母东西的参考值﹐而后才跟母东西勾通。即使不透过shared步调,也不妨采用下述本领:'ex04.basimports system.componentmodelimports system.drawingimports system.winforms'----------------------------------------------------class room protected rsize as double public overridable sub create() end sub public function getsize() as double getsize = rsize end functionend classclass desk protected dsize as double protected mymother as room public sub create(byval mo as room) mymother = mo dsize = mymother.getsize() * 0.18 end sub public function getsize() as double getsize = dsize end functionend class'----------------------------------------------------class myroom inherits room private rd as desk public sub new() rd = new desk() me.create() end sub public overrides sub create() rsize = 100 rd.create(me) end sub public sub show() messagebox.show("room size: " + str(rsize)) messagebox.show("desk size: " + str(rd.getsize())) 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 r as new myroom() r.show() end subend class此步调输入: room size: 100 desk size: 18desk之东西含个参考 mymother ﹐指向其母东西。这项联系是在母子东西皆建好时﹐才由create()步调所创造的。所以﹐创造出母子东西之联系﹕综上所述,当myroom类型运用如次训令── private rd as new desk()时,才必需把new()与create()辨别。即使运用如次训令──private rd as deskpublic sub new() rd = new desk() .....end sub就不用辨别了,因为是:new()与create()的实行程序是普遍的,比方两者可兼并如次的vb步调:'ex05.basimports system.componentmodelimports system.drawingimports system.winforms'----------------------------------------------------class room protected rsize as double public function getsize() as double getsize = rsize end functionend classclass desk protected dsize as double protected mymother as room public sub new(byval mo as room) mymother = mo dsize = mymother.getsize() * 0.18 end sub public function getsize() as double getsize = dsize end functionend class'----------------------------------------------------class myroom inherits room private rd as desk public sub new() rsize = 100 rd = new desk(me) end sub public sub show() messagebox.show("room size: " + str(rsize)) messagebox.show("desk size: " + str(rd.getsize())) 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 r as new myroom() r.show() end subend class此步调输入: room size: 100 desk size: 183. 特类型接受与母子东西 您已很熟习爷儿俩类型联系了﹐这接受联系的另部分是母子东西联系。比方﹐'ex06.basimports system.componentmodelimports system.drawingimports system.winforms'----------------------------------------------------class room private mother as myroom public sub new(byval mo as myroom) mother = mo end sub public function getid() as string getid = mother.yourid() end functionend classclass myroom inherits room public sub new() mybase.new(me) end sub public function yourid() as string yourid = "vip888" end functionend 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 r as new myroom() messagebox.show(r.getid()) end subend class此步调输入﹕ vip888myroom()建构者new()将me值给 room的mother参考。所以mother参考到母东西r。room类型是myroom之父类型﹐但room之东西却是myroom之子东西。 即使将上述room的训令:public function getid() as string getid = mother.yourid() end function变动为:public function getid() as string getid = me.yourid() 'error!!end function就错了。由于room类型里没有设置yourid()步调。究竟上,vb的接受体制里仍旧有母东西的参考值了,vb的overridable体制即是鉴于它而呼唤到子类型(母东西)的步调的。比方上述步调十分于:'ex07.basimports system.componentmodelimports system.drawingimports system.winforms'----------------------------------------------------class room public function getid() as string getid = me.yourid() end function public overridable function yourid() as string end functionend classclass myroom inherits room public sub new() mybase.new() end sub public overrides function yourid() as string yourid = "vip888" end functionend 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 r as new myroom() messagebox.show(r.getid()) end subend class 此步调输入﹕ vip888上述ex07.bas比ex06.bas好的场合是: ex06.bas步调的room类型内里用到myroom的称呼。而ex07.bas步调的room类型内里并没用到myroom的称呼,所以room类型不妨先安排,myroom类型能厥后才安排,这是接受体制的手段之一。然而,即使您确定不想用接受与overridable观念的话,可运用vb的interface体制来革新ex06.bas步调,如次:'ex08.basimports system.componentmodelimports system.drawingimports system.winforms'----------------------------------------------------interface imyroom function yourid() as stringend interfaceclass room private mother as imyroom public sub new(byval mo as imyroom) mother = mo end sub public function getid() as string getid = "roomid: " + mother.yourid() end functionend classclass myroom implements imyroom private base as room public sub new() base = new room(me) end sub public function yourid() as string implements imyroom.yourid yourid = "vip888" end function public function getid() as string getid = base.getid() end functionend 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 r as new myroom() messagebox.show(r.getid()) end subend class此步调输入﹕ roomid: vip888 普遍运用委派(delegation)来包办接受时,常用的手法。但是上述ex08.bas步调的myroom类型内里用到了room称呼,即使您不蓄意如许,可设置一个iroom接口,供myroom类型运用,如次步调:'ex09.basimports system.componentmodelimports system.drawingimports system.winforms'----------------------------------------------------interface imyroom function yourid() as stringend interfaceinterface iroom function getid() as string sub connect(byval m as imyroom)end interfaceclass room implements iroom private motherobject as imyroom public function getid() as string implements iroom.getid getid = motherobject.yourid() + " ***" end function public sub connect(byval m as imyroom) implements iroom.connect motherobject = m end subend classclass myroom implements imyroom private base as iroom public sub connect(byval r as iroom) base = r r.connect(me) end sub public function yourid() as string implements imyroom.yourid yourid = "dog888" end function public function getid() as string getid = base.getid() end functionend 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 my as new myroom() dim base as new room() my.connect(base) messagebox.show(my.getid()) end subend class此步调输入﹕ roomid: vip888 room类型内里没用到myroom称呼,并且myroom类型里没有效到room名,所以两个类型可独力安排。这是散布式组件软硬件,如mts(microsoft transaction server)等体例里的常

热门阅览

最新排行

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