大雀软件园

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

VB.Net中文教程(5)程序多重定义

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

1. 步调分子的多重设置 「步调多重设置」(function overloading)又称为「步调反复设置」。它让东西更具弹性﹐能处置百般化之消息。这看法源于凡是生存体味。比方﹐咱们常说﹕ ◎ 猫 玩 绣球 ◎ 猫 玩 老鼠 ◎ 猫 玩 鱼猫玩绣球与玩老鼠之玩法不尽沟通。但何故运用同一动词──「玩」呢﹖大概人们觉得其手段是普遍的﹕猫赢得痛快。上述的「猫」为类型﹐而某只猫是东西。比方﹕加菲猫是东西﹐它可接收消息── 个中﹐「玩」代办着举措和进程﹐而绣球、老鼠及鱼则是「玩」之东西。回顾﹐在步调中﹐「步调」代办一项举措及进程﹐而「自变量值」则为步调之处置东西。因之﹐上海图书馆可表白为── 图1、 play()之多重设置 oop 步调安排之理念为﹕让步调之写法与人们凡是生存体味符合﹐所以安排个play()步调﹐让它能接收各别型态之材料做为处置东西。上述play()已具「多重设置」﹐其特性是── 1. 步调称呼沟通﹐比方﹕play()。 2. 自变量各别﹐比方﹕老鼠和鱼。因猫玩绣球和玩老鼠的本领略有各别﹐比方老鼠是活的而绣球是死的﹐其玩的进程亦不尽沟通。为了表白举措与进程之各别﹐play()步调内之训令也有所各别。比方﹕写vb步调时﹐其方法必需是── class cat public overloads sub play(绣球) 训令 ....... end sub public overloads sub play(老鼠) 训令 ....... end sub public overloads sub play(鱼) 训令 ....... end sub end class 这即是「步调分子多重设置」了。cat 类型含有三种play()之设置﹐其自变量各别并且里面训令亦不沟通。所以play()步调能接收各别之自变量﹐并实行各别之训令﹐使得play()具弹性了。请看个步调──'ex01.basimports system.componentmodelimports system.drawingimports system.winforms'-------------------------------------------------------class example public overloads sub display() messagebox.show("****") end sub public overloads sub display(byval r as integer) messagebox.show(str(r)) end sub public overloads sub display(byval f as double) messagebox.show(str(f + 2)) end sub public overloads sub display(byval s as string) messagebox.show(s) 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 example() a.display() a.display("taiwan") a.display(80) a.display(100.5) end subend class此步调输入如次﹕ ****taiwan80102.5 这example类型比拟特出﹐没有材料分子﹔但含有一个步调分子叫display() 。而display()有 4个各别之本子(设置)﹐可任君(计划机)抉择。计划机藉比对自变量来抉择「最相称」之display()步调。 比方﹕计划机实行到训令── a.display("taiwan") 因为自变量── "taiwan"是字符串﹐其型态应配上string﹐以是计划机抉择而且实行第 4个步调── display( byval s as string ) 。同理﹐当计划机实行到训令── a.display(100.5) 因为自变量──100.5之型态为double﹐以是计划机选上并实行第 3个display()步调── display(byval f as double )。同一步调称呼但罕见个各别之设置﹐各有各别之自变量及里面训令﹐此种局面即是「步调的多重设置」。请再看个例子──'ex02.basimports system.componentmodelimports system.drawingimports system.winforms'--------------------------------------------------class sum private s as integer public overloads sub add() s = 3 + 5 end sub public overloads sub add(byval x as integer) s = x + 5 end sub public overloads sub add(byval x as integer, byval y as integer) s = x + y end sub public sub show() messagebox.show("sum = " + str(s)) 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 sum() a.add() a.show() a.add(80) a.show() dim b as new sum() b.add(100, 27) b.show() end subend class此步调输入如次﹕ sum = 8 sum = 85 sum = 127当计划机实行到训令── b.add( 100, 27 ),因为有两个自变量﹐且型态皆为integer﹔计划机就选上并实行第三个add() 步调。此时计划机把100传给x﹐而27传给y。这多重设置之看法﹐也常用来建构者步调上。比方﹕'ex03.basimports system.componentmodelimports system.drawingimports system.winforms'---------------------------------------------------class rectangle private height as integer, width as integer public overloads sub new() height = 0 width = 0 end sub public overloads sub new(byval k as integer) height = k width = k end sub public overloads sub new(byval h as integer, byval w as integer) height = h width = w end sub public sub showarea() messagebox.show("area = " + str(height * width)) 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 r1 as new rectangle() dim r2 as new rectangle(8) dim r3 as new rectangle(5, 6) r1.showarea() r2.showarea() r3.showarea() end subend class此步调输入﹕ area = 0 area = 64 area = 30颁布东西时﹐若未赋予自变量值﹐计划机呼唤new()﹔若给一个自变量值── 8﹐就呼唤 new(byval k as integer) ﹔若给二个自变量值──5 及 6﹐则呼唤new(byval h as integer, byval w as integer)。请再看一个例子:'ex04.basimports system.componentmodelimports system.drawingimports system.winforms'-------------------------------------------------------------------------------------------------class rectangle private height as integer, width as integer public sub new(byval h as integer, byval w as integer) height = h width = w end sub public function area() as integer area = height * width end function public overloads function comparewith(byval a as integer) as integer dim d as integer d = area() - a if d <> 0 then comparewith = 1 else comparewith = 0 end if end function public overloads function comparewith(byval r as rectangle) as integer dim d as integer d = area() - r.area() if d <> 0 then d = 1 end if comparewith = d end function public overloads function comparewith( byval x as rectangle, byval y as rectangle) as integer dim d as integer d = x.area() - y.area() if d <> 0 then d = 1 end if comparewith = d 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 r1 as new rectangle(10, 50) dim r2 as new rectangle(20, 25) if r1.comparewith(400) = 0 then messagebox.show("equal") else messagebox.show("not equal") end if if r1.comparewith(r2) = 0 then messagebox.show("equal") else messagebox.show("not equal") end if if r1.comparewith(r1, r2) = 0 then messagebox.show("equal") else messagebox.show("not equal") end if end subend class此步调输入﹕ not equal equal equal如许﹐comparewith()步调就有三种用处了﹔即使您想减少其它用处﹐可纵情地再设置它。r1.comparewith(400)呼唤第1个comparewith(),比比看r1表面积能否大于400;r1.comaprewith(r2) 呼唤第2个comparewith(),比比看r1表面积能否大于r2的表面积;r1.comaprewith(r1, r2) 比比看r1表面积能否大于r2的表面积。即使没有运用多重设置本领,这三个步调称呼不许沟通。比方﹕上述步调可改写为──'ex05.basimports system.componentmodelimports system.drawingimports system.winforms'-------------------------------------------------------------------------------------------class rectangle private height as integer, width as integer public sub new(byval h as integer, byval w as integer) height = h width = w end sub public function area() as integer area = height * width end function public function comparewithinteger(byval a as integer) as integer dim d as integer d = area() - a if d <> 0 then d = 1 end if comparewithinteger = d end function public function comparewithrectangle(byval r as rectangle) as integer dim d as integer d = area() - r.area() if d <> 0 then d = 1 end if comparewithrectangle = d end function public function comparetworectangle( byval x as rectangle, byval y as rectangle) as integer dim d as integer d = x.area() - y.area() if d <> 0 then d = 1 end if comparetworectangle = d 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 r1 as new rectangle(10, 50) dim r2 as new rectangle(20, 25) if r1.comparewithinteger(400) = 0 then messagebox.show("ggg equal") else messagebox.show("not equal") end if if r1.comparewithrectangle(r2) = 0 then messagebox.show("equal") else messagebox.show("not equal") end if if r1.comparetworectangle(r1, r2) = 0 then messagebox.show("equal") else messagebox.show("not equal") end if end subend class此步调输入﹕not equalequalequal因为各步调称呼不沟通,您就得回顾各步调之名字﹐徒增回顾承担并且容易犯错﹐并不对乎人们生存风气。因之﹐vb的多重设置看法﹐能减少步调之弹性及关心感。 步调多重设置景象并不限于简单类型之内,也不妨爆发于爷儿俩类型之间。比方:'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 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 classteacher类型从person接受了setvalue() ── setvalue(byval na as string, byval a as integer)本人又反复设置一个新的setvalue()步调── setvalue(byval na as string, byval a as integer, byval no as integer)公有两个setvalue()可用。训令x.setvalue("alvin", 32)呼唤第1个setvalue();训令y.setvalue("tom", 36, 11138)呼唤第1个setvalue()。 兹在夸大一个子类型如次:'ex07.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 overridable 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 overrides sub display() 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 overrides sub display() 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.display() end subend class此步调输入﹕ name: alvin age: 32 name: tom age: 36 studno: 11138 此时﹐student 类型含有两个setvalue()步调,一个是从person类型接受而来,另一个是自行设置的。即使上述form1_click()内的训令变动如次: dim y as new student() y.setvalue("tom", 36, 5000.25) 'error! y.display()固然setvalue("tom", 36, 5000.25)符合teacher的setvalue()步调的参数,然而student并非person的子类型,没有接受student的setvalue(),以是错了。n

热门阅览

最新排行

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