大雀软件园

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

VB.Net中文教程(4) 类别继承(Inheritance)关系

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

1. 类型之接受 类型之间﹐有些互为独力﹐有些具备出色联系。兹引见类型间罕见的联系──「爷儿俩」联系﹔因为后代常接受双亲之心理或情绪特性﹐以是又称此联系为「接受」(inheritance) 联系。类型间之出色联系﹐把关系的类型构造起来﹐而且构造步调内之东西。若步调内之东西毫无构造﹔表露一片散沙﹐就不是好步调。完备之vb步调﹐必需关心类型间之联系﹐东西是有构造的。 即使 a类型「接受」 b类型﹐则称 a为「子类型」(subclass)﹐也称b 为「父类型」(superclass)﹐亦即 b为 a之父类型﹐a 为 b之子类型。在 c++中﹐父类型又称为「普通类型」(base class)﹐子类型又称为「派生类型」(derived class) 。大概您感触「接受」之看法很生疏﹐不知怎样看出类型间之接受联系。别担忧﹐有个简片面法﹕下列两报告之意旨沟通── (1) a 为 b之子类型。 (2) a 为 b之一种(a kind of) 特出类型。按照报告 (2)能简单找到爷儿俩联系。比方﹕肯尼士(kennex)消费高品德拍子﹐拍子分两种﹕网拍子与羽拍子。此后句子得悉﹕网拍子为一种(a kind of) 拍子﹐羽拍子亦为一种拍子。因之﹐网拍子为拍子之子类型﹐羽拍子亦为拍子之子类型﹐亦即拍子是父类型。以次图标之﹕ 图1、 普通类型与派生类型 即使安排步调来记载拍子之消费景象﹐则步调应设置普通类型──拍子﹐以及两派生类型──网拍子及羽拍子。步调应藉接受联系将三类型构造起来。除去货色(如拍子、公共汽车等)外﹐人也有接受联系。比方﹕书院职员囊括弟子、教授及人员﹐教授又分为现任教授及兼任教授。书院的人事软硬件体例﹐应设置类型联系如次﹕ 图2、 三代接受联系 步调不只要设置类型﹐也要设置其接受联系。2. 设置接受联系 前方各章里﹐已引见怎样设置类型﹔本节将报告您怎样设置类型之接受联系。兹举例证明之﹕步调的安排进程是﹕ step 1. 设置普通类型(父类型)。如﹕ class person ‥‥ end class step 2. 设置派生类型(子类型)。如﹕ class teacher inherits person ‥‥ end class class student inherits person ‥‥ end class inherits字眼之后为父类型称呼。它表白了:teacher 为person之子类型﹐且 student为person之子类型。此刻﹐已领会怎样表白接受联系了﹔但是﹐子类型从父类型接受什么货色呢﹖类型包括「材料」及「步调」。因之﹐子类型接受父类型之材料及步调。此刻﹐请看看怎样接受材料及步调。下述步调设置 person 类型﹐含有 2项材料及 3个步调﹕'ex01.basimports system.componentmodelimports system.drawingimports system.winforms'-----------------------------------------------------------------------------------------class person private name as string private age as integer public sub setvalue(byval na as string, byval a as integer) name = na age = a end sub public function birthyear() as integer birthyear = 2001 - age end function public sub display() messagebox.show("name: " + name + " age: " + str(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 mike as person mike = new person() mike.setvalue("mike", 45) mike.display() messagebox.show("birthyear: " + str(mike.birthyear())) end subend class此步调输入如次﹕ name: mike age: 45birthyear: 1956 所谓接受材料﹐表白接受材料分子之设置﹐而不是接受材料之值﹐请详加辨别之。类型设置材料分子(含型态及称呼)﹐东西出生后﹐东西内才有材料值。以是「类型接受」乃接受类型之设置﹐不是接受东西之值。也即是说﹕若父类型设置name及 age两个材料分子﹐则子类型天才就具有此两个材料分子﹐以是子类型不需设置它们。所谓接受步调﹐表白子类型天才就具有父类型设置之步调分子。比方﹕person的子类型天才就具备 setvalue()、birthyear()及display()步调。此刻﹐就来设置person之子类型。'ex02.basimports system.componentmodelimports system.drawingimports system.winforms'-----------------------------------------------------------------------------------------public class person private name as string private age as integer public sub setvalue(byval na as string, byval a as integer) name = na age = a end sub public function birthyear() as integer birthyear = 2001 - age end function public sub display() messagebox.show("name: " + name + " age: " + str(age)) end subend classpublic class teacher inherits personend 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 steven as teacher steven = new teacher() steven.setvalue("steven", 35) steven.display() end subend class此步调输入如次﹕ name: steven age: 35 外表上teacher 类型中﹐不决义数据项及步调﹐但究竟上已具有name及 age两个材料分子﹐也具有 setvalue()、birthyear()及display() 三个步调分子。因之﹐steven为teacher类型之东西﹐它能呼唤setvalue()及display()步调。在运用上﹐子类型常常具有本人的数据项及步调﹐使其有别于父类型。比方﹕'ex03.basimports system.componentmodelimports system.drawingimports system.winforms'------------------------------------------------------------------------------------------------------public class person private name as string private age as integer public sub setvalue(byval na as string, byval a as integer) name = na age = a end sub public function birthyear() as integer birthyear = 2001 - age end function public sub display() messagebox.show("name: " + name + " age: " + str(age)) end subend classpublic class teacher inherits person private salary as decimal public sub tr_setvalue( byval na as string, byval a as integer, byval sa as decimal) setvalue(na, a) salary = sa end sub public sub pr() mybase.display() messagebox.show("salary: " + str(salary)) 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 steven as teacher steven = new teacher() steven.tr_setvalue("steven", 35, 35000) steven.pr() end subend class此步调输入如次﹕ name: steven age: 35salary: 35000此刻﹐teacher 类型含有三个材料分子﹕ 1. name ──从person类型接受而来。 2. age ──从person类型接受而来。 3. salary ──本人设置的。其余﹐也含有五个步调分子﹕ 1. setvalue() ──从person接受而来。 2. birthyear()──从person接受而来。 3. display() ──从person接受而来。 4. tr_setvalue()──本人设置的。 5. pr()──本人设置的。 因为setvalue()为 teacher之步调分子﹐以是tr_setvalue()能径直呼唤setvalue()来设定name及age值﹔之后﹐tr_setvalue()本人设定salary之值。同理﹐pr()能径直呼唤 display()来表露name及age之实质﹔之后﹐pr()本人输入salary之值。大概﹐您会问及﹕子类型本人设置之步调﹐能否能与父类型之步调同称呼呢﹖谜底是确定的﹐并且是罕见之写法。即使称呼沟通然而参数不普遍(像参数个数各别或参数型态各别),即是爷儿俩类型之间的步调多重设置(procedure overloading)了,必需写上overloads字眼。比方﹕上述步调十分于──'ex04.basimports system.componentmodelimports system.drawingimports system.winforms'------------------------------------------------------------------------------------------class person private name as string private age as integer public sub new() end sub public sub setvalue(byval na as string, byval a as integer) name = na age = a end sub public function birthday() as integer birthday = 2001 - age end function public sub display() messagebox.show("name: " + name + " age: " + str(age)) end subend classclass teacher inherits person private salary as decimal public overloads sub setvalue( byval na as string, byval a as integer, byval sa as decimal) setvalue(na, a) salary = sa end sub public sub pr() mybase.display() messagebox.show("salary: " + str(salary)) 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 steven as teacher steven = new teacher() steven.setvalue("steven", 35, 35000) steven.pr() end subend class此步调输入如次﹕ name: steven age : 35 salary: 35000此种情景﹐teacher 类型具有两个 setvalue()步调﹐一个由person接受而来﹐一个是本人设置的。同声﹐也具有两个display()步调。此时﹐计划机怎样辨别它们呢﹖计划机按照步调的参数来确定呼唤那一个步调,当您写成 steven.setvalue("steven", 35, 35000)时,此setvalue()是子类型本人的setvalue()。而写成setvalue(na,a)时,则是由person类型接受而来之setvalue()步调。此vb步调已设置如次之类型联系﹕接下来﹐再为person设置一个子类型── student。步调如次﹕'ex05.basimports system.componentmodelimports system.drawingimports system.winforms'------------------------------------------------------------------------------------------public class person private name as string private age as integer public sub new() end sub public sub setvalue(byval na as string, byval a as integer) name = na age = a end sub public function birthday() as integer birthday = 2001 - age end function public sub display() messagebox.show("name: " + name + " age: " + str(age)) end subend classpublic class teacher inherits person private salary as decimal public overloads sub setvalue( byval na as string, byval a as integer, byval sa as decimal) setvalue(na, a) salary = sa end sub public sub pr() mybase.display() messagebox.show("salary: " + str(salary)) end subend classpublic class student inherits person private student_number as integer public overloads sub setvalue( byval na as string, byval a as integer, byval no as integer) setvalue(na, a) student_number = no end sub public sub pr() mybase.display() messagebox.show("studno: " + str(student_number)) 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() x.setvalue("alvin", 32) dim y as new student() y.setvalue("tom", 36, 11138) x.display() y.pr() end subend class此步调输入﹕ name: alvin age: 32 name: tom age: 36 studno: 11138 此时﹐student 类型含有name、age 及student_number三个材料分子。并且具有 setvalue()、person.setvalue() 、display()、pr() 及birthyear()五个步调分子。所以创造了如次之接受联系﹕ x 东西含name及age 两项材料﹐训令──x.setvalue ("alvin", 32)﹐将材料惠存x 东西中。因student接受person﹐以是y 东西内含name、age 及student_number三项材料﹐训令──y.setvalue("tom", 36, 11138) 将材料惠存y 东西中。上述 setvalue()步调之功效是﹕设定东西之初值。可由建构者(constructor) 包办之。以是此student之设置十分于──'ex06.basimports system.componentmodelimports system.drawingimports system.winforms'----------------------------------------------------public class person private name as string private age as integer public sub new() end sub public sub setvalue(byval na as string, byval a as integer) name = na age = a end sub public function birthday() as integer birthday = 2001 - age end function public sub display() messagebox.show("name: " + name + " age: " + str(age)) end subend classpublic class teacher inherits person private salary as decimal public overloads sub setvalue(byval na as string, byval a as integer, byval sa as decimal) setvalue(na, a) salary = sa end sub public sub pr() mybase.display() messagebox.show("salary: " + str(salary)) end subend classpublic class student inherits person private student_number as integer public sub new(byval na as string, byval a as integer, byval no as integer) setvalue(na, a) student_number = no end sub public sub pr() mybase.display() messagebox.show("studno: " + str(student_number)) 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() x.setvalue("alvin", 32) dim y as new student("tom", 36, 11138) x.display() y.pr() end subend class此步调输入﹕ name: alvin age: 32 name: tom age: 36 studno: 11138子类型之建构者呼唤父类型之setvalue()来设定东西初值。请提防看y 东西之出生进程﹕y 出生时﹐建构者── public sub new(byval na as string, byval a as integer, byval no as integer) setvalue(na, a) student_number = no end sub它先呼唤父类型的天然建构者person.new()一道出生y 东西。此时﹐person.new()出生如次﹕接受局部是person.new()出生的。person.new()工作实行了﹐轮到student的建构者自己出生(夸大)如次﹕ 出生结束﹐发端实行student建构者内之训令以设定y 之初值。开始呼唤父类型的setvalue() ﹐所以setvalue()将值惠存y 如次﹕结果﹐实行训令── student_number = no﹐设定student_number数据之初值﹕所以﹐student的建构者出生y 东西﹐也设定初值﹐功成完备。 上述的student建构者呼唤父类型之天然建构者(由于person类型不决义建构者﹐计划机机动爆发天然建构者)来扶助出生东西。即使person类型设置了建构者﹐student建构者便能径直呼唤person建构者了。比方﹕'ex07.basimports system.componentmodelimports system.drawingimports system.winforms'-----------------------------------------------------------------------------------------public class person private name as string private age as integer public sub new(byval na as string, byval a as integer) name = na age = a end sub public function birthday() as integer birthday = 2001 - age end function public sub display() messagebox.show("name: " + name + " age: " + str(age)) end subend classpublic class teacher inherits person private salary as decimal public sub new(byval na as string, byval a as integer, byval sa as decimal) mybase.new(na, a) salary = sa end sub public sub pr() mybase.display() messagebox.show("salary: " + str(salary)) end subend classpublic class student inherits person private student_number as integer public sub new(byval na as string, byval a as integer, byval no as integer) mybase.new(na, a) student_number = no end sub public sub pr() mybase.display() messagebox.show("studno: " + str(student_number)) 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("alvin", 32) dim y as new student("tom", 36, 11138) x.display() y.pr() end subend class此步调输入﹕ name: alvin age: 32 name: tom age: 36 studno: 11138子类型之建构者── public sub new(byval na as string, byval a as integer, byval sa as decimal) ...... end sub先呼唤父类型之建构者── public sub new(byval na as string, byval a as integer) ...... end sub扶助出生东西如次﹕ 接着﹐实行父类型person的建构者内之训令﹐设定初值如次﹕person建构者处事实行了﹐轮到student建构者的处事﹐夸大东西如次﹕出生结束﹐实行student建构者内之训令﹐设定初值﹕所以﹐y 东西出生结束了。 子类型建构者呼唤父类型建构者之形式﹕比方﹐student建构者内的mybase.new(na, a)训令将na及a值传播给person建构者﹐所以person建构者设定name及age之初值。除去呼唤person建构者除外﹐还实行本人的训令──student_number = no ﹐设定student_number之初值。student类型之东西含有三个材料分子﹐个中name及 age是由person类型接受而来﹐就藉person建构者设定其初值。至于student类型本人设置之材料分子student_number﹐就由本人的训令──student_number = no设定其初值。3. 藉「接受」夸大步调 步调之兴盛是循序渐进的﹐软硬件内之类型需随企业之生长而连接夸大。类型夸大常源于﹕(1) 企业生长了﹐爆发了新类型。 比方﹕某计划机公司﹐往日创造巨型及迷你计划机﹐此刻新推出部分计划机。就必需创造新子类型如次﹕ 图3、 步调夸大本领──派生子类型 再如﹐某钱庄往日供给空头支票帐户(checking account)及积聚帐户(saving account)给存户﹐此刻拟减少按期帐户(cd account)。就得新增子类型如次﹕ 这新类型cd_account接受父类型saving_account及account 的材料及步调。比方﹐account 之设置如次﹕'ex08.basimports system.componentmodelimports system.drawingimports system.winforms'-----------------------------------------------------------------------------------class account private shared next_number as integer protected acc_no as integer protected name as string protected balance as decimal shared sub new() next_number = 1000 end sub public sub new(byval na as string) name = na balance = 0 acc_no = next_number next_number = next_number + 1 end sub public sub deposit(byval money as decimal) balance = balance + money end sub public sub withdraw(byval money as decimal) balance = balance - money end sub public sub close() acc_no = -1 end subend classclass saving_account inherits account private interest_rate as double public sub new(byval na as string, byval rate as double) mybase.new(na) me.interest_rate = rate end sub public function comp_interest() as double comp_interest = balance * interest_rate end function public sub display() messagebox.show("acc_no: " + str(mybase.acc_no)) messagebox.show("余额: " + str(me.balance) + " 本钱: " + str(comp_interest())) 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 saving_acc as new saving_account("tom bush", 0.085) dim account as saving_account account = new saving_account("linda lin", 0.008) account.deposit(5000) account.withdraw(2500) account.display() account.close() end subend class此步调输入﹕ acc_no: 1001 余额: 2500 本钱: 20 因saving_account接受account ﹐以是saving_account类型亦具有deposit() 、withdraw()及close() 三个步调。请换个观点﹐从saving_account类型之安排者来看﹐他透过接受联系来「滥用」父类型之步调。「滥用」又称为「再运用」(resue) ﹐能使得子类型大略但却强而有力。就如saving_account类型显得比account 类型大略﹐但它所有却含有deposit() 、withdraw()等6 个步调﹐功效比account 类型宏大很多。对于训令──account.deposit(5000) 而言﹐其有两种意旨﹕ i. saving_account接受account ﹐以是saving_account具备deposit() 步调﹐可将5000惠存account 东西。 ii. saving_account为account 之子类型﹐它能滥用(再运用)父类型之deposit() 步调﹐来将5000惠存account 东西。 这两者是一体的两面﹐接受功效让软硬件师能连接新增子类型﹐连接再运用现有类型之步调﹐来简化子类型之搀杂度。接受使得子类型具有太爷类型之百般功效﹐且再减少新功效﹔再运用使得子类型尽管委派太爷类型来控制处事﹐本人以逸待劳。所以﹐善用接受功效﹐常常心存再运用看法﹐便能安排大略却功效宏大的子类型了。而步调员的功效提高了﹐软硬件之本钱低沉了﹐此乃oop 的理念目的之一。兹再夸大一个子类型cd_account如次﹕'ex09.basimports system.componentmodelimports system.drawingimports system.winforms'--------------------------------------------------------------------------------class account private shared next_number as integer protected acc_no as integer protected name as string protected balance as decimal shared sub new() next_number = 1000 end sub public sub new(byval na as string) name = na balance = 0 acc_no = next_number next_number = next_number + 1 end sub public sub deposit(byval money as decimal) balance = balance + money end sub public sub withdraw(byval money as decimal) balance = balance - money end sub public sub close() acc_no = -1 end subend classclass saving_account inherits account private interest_rate as double public sub new(byval na as string, byval rate as double) mybase.new(na) me.interest_rate = rate end sub public function comp_interest() as double comp_interest = balance * interest_rate end function public sub display() messagebox.show("acc_no: " + str(mybase.acc_no)) messagebox.show("余额: " + str(me.balance) + " 本钱: " + str(comp_interest())) end subend classclass cd_account inherits saving_account public sub new(byval na as string, byval rate as double, byref b as decimal) mybase.new( na, rate + 0.003 ) mybase.deposit(b) 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 cd_account a = new cd_account("linda lin", 0.008, 2500) a.display() a.close() end subend classcd_account除去在设置本人的new()除外,皆接受saving_account的分子。请您提防观赏下述步调,看看跟上一个步调有何巧妙分别?'ex10.basimports system.componentmodelimports system.drawingimports system.winforms'--------------------------------------------------------------------------------class account private shared next_number as integer protected acc_no as integer protected name as string protected balance as decimal shared sub new() next_number = 1000 end sub public sub new(byval na as string) name = na balance = 0 acc_no = next_number next_number = next_number + 1 end sub public sub deposit(byval money as decimal) balance = balance + money end sub public sub withdraw(byval money as decimal) balance = balance - money end sub public sub close() acc_no = -1 end subend classclass saving_account inherits account private interest_rate as double public sub new(byval na as string, byval rate as double) mybase.new(na) me.interest_rate = rate end sub public function comp_interest() as double comp_interest = balance * interest_rate end function public sub display() messagebox.show("acc_no: " + str(mybase.acc_no)) messagebox.show("余额: " + str(me.balance) + " 本钱: " + str(comp_interest())) end subend classclass cd_account private acc as saving_account public sub new(byval na as string, byval rate as double, byref b as decimal) acc = new saving_account(na, rate + 0.003) acc.deposit(b) end sub public function comp_interest() as double comp_interest = acc.comp_interest() end function public sub display() acc.display() end sub public sub close() acc.close() 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 cd_account a = new cd_account("linda lin", 0.008, 2500) 'a.deposit(5000) 'error here!! a.display() a.close() end subend class(2) 情况变换了﹐必需矫正旧类型。 比方﹕软硬件内含sales 类型如次──'ex11.basimports system.componentmodelimports system.drawingimports system.winforms'----------------------------------------------------------------------------------class sales protected sum as double public sub new() sum = 0 end sub public overridable sub input(byval amount as double) sum = sum + amount end sub public sub display() messagebox.show("the sum is: " + str(sum)) 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 sales() x.input(3000.5) x.input(7999.5) x.input(15000) x.display() end subend class此步调输入如次﹕the sum is: 26000input() 输出出卖金额﹐且将金额加总起来﹐存于 sum变量中。display() 表露出总金额。跟着企业之生长﹐常常对软硬件之功效有新的需要。比方﹕蓄意步调输入平衡出卖额。此时﹐必需革新从来的软硬件﹐其本领有二﹕(i) 改写 sales类型之设置。 此为保守软硬件之保护本领﹐就像一座衡宇﹐已不敷运用﹐所以用推土机推掉﹐重修之。常常这种本领并不佳﹐因为是﹕旧的软硬件有些功效仍旧可用﹐废除重修简直怅然。若不想重修﹐只想去窜改它﹐然而软硬件若很宏大﹐得滥用人工去领会旧软硬件的构造﹐弄通了之后﹐本领加以矫正﹐明显不财经。进一步想﹐于矫正之进程中﹐不经意而变动旧软硬件内仍可用之局部﹐则从来精确之功效﹐相反有遭妨害之虞﹗(ii) 藉接受联系矫正 sales类型。 亦即设置 sales之子类型﹐来矫正及夸大 sales之功效。此时﹐并未变动sales 类型之设置﹐以是不会妨害 sales之功效。既是生存原有的杰出功效﹐又能减少新的功效﹐难道面面俱到呢﹖此「旧干发新枝」之场合即是千年神木得以繁茂且绿意盎然之原因﹐也恰是 oop魅力的泉源。此刻﹐就为 sales设置个子类型叫 sales_new如次﹕'ex12.basimports system.componentmodelimports system.drawingimports system.winforms'------------------------------------------------------------------------------------class sales protected sum as double public sub new() sum = 0 end sub public overridable sub input(byval amount as double) sum = sum + amount end sub public sub display() messagebox.show("the sum is: " + str(sum)) end subend classclass sales_new inherits sales protected number as integer public sub new() number = 0 end sub public overrides sub input(byval amount as double) mybase.input(amount) number = number + 1 end sub public sub display_average() dim avg as double if number > 0 then avg = sum / number messagebox.show("the average is : " + str(avg)) end if 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 sales_new() x.input(3005.5) x.input(5994.5) x.input(15012.6) x.display() x.display_average() end subend class此步调输入﹕ the sum is: 24012.6 the average is: 8004.2 子类型减少了新数据项number﹐从新设置了input()﹐使新input()生存父类型内之input()功效﹐并加上number = number +1之演算。保持了父类型之display()﹐但减少了display_average()步调。依此本领﹐软硬件天然日益繁茂﹐且持久弥新﹗藉接受联系连接夸大乃为oop之要害本领﹐盼您善用之。n

热门阅览

最新排行

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