VB.Net中文教程(9) 重新定义(Overriding)程序
2024-07-10 13:02:42
供稿:网友
主题: 重新定义(overriding)程序
?????????? 内容 ??????????
v 1. 重新定义程序
1. 重新定义(override)程序
在应用上﹐常见如下之情况﹕子类别从父类别继承之程序﹐并不合乎子类别之需要。此时可设计新程序取代之。
图1、程序成员之重新定义
例如﹐salesperson类别含有bonus()程序﹔salesmanager由 salesperson继承而得bonus()程序。bonus()能计算销售人员之红利。然而﹐一般销售员与销售经理之红利计算方法不同﹔所以﹐salesperson之bonus()显然并不适用于salesmanager。这salesmanager类别必须定义适用的bonus()求算销售经理之红利。子类别不满意继承之程序而自行定义程序取代之﹐此情形称为「再定义」(redefinition)或「重新定义」(overriding)。自行定义程序与继承程序之名称相同。请看个程序﹕
'ex01.bas
imports system.componentmodel
imports system.drawing
imports system.winforms
'-----------------------------------------------------------------------------------------
class salesperson
protected totalsales as double
public sub new(byval t as double)
totalsales = t
end sub
public overridable function bonus() as double
bonus = totalsales * 0.008
end function
end class
class salesmanager
inherits salesperson
public sub new(byval t as double)
mybase.new(t)
end sub
public overrides function bonus() as double
bonus = totalsales * 0.008 + 1000
end function
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 jim as new salesperson(50000)
messagebox.show("jim's bonus: " + str(jim.bonus()))
dim tom as new salesmanager(45000)
messagebox.show("tom's bonus: " + str(tom.bonus()))
end sub
end class
此程序输出如下﹕ jim's bonus: 400
tom's bonus: 1360
因jim为salesperson类别之对象﹐则jim.bonus()所指的是salesperson之bonus()程序。salesmanager类别继承此bonus()﹐且自己定义新的bonus()程序。此新程序取代了继承之程序。因之﹐alvin.bonus()是指salesmanager自己定义之bonus()程序。
「重新定义」(overriding)的过程中﹐必定也产生「重复定义」(overloading) 之现象。前者是针对「父子」类别之间﹐子类别有「修正」(refinement)或「取代」(replacement) 之意味时﹐才定义同名称之程序来取代父类别之程序。至于重复定义则范围较广﹐在类别内也能重复定义程序﹐父子类别之间也能重复定义程序﹐甚至互为独立的两类别之间也能发生重复定义之现象﹔重复定义之后新程序与原程序之间﹐在「涵义」(semantic)上仅类似,但并不必然相同,例如bonus(byval y as year)与bonus(byval y as employee)两者涵义不尽相同。
然而重新定义程序﹐例如上述的 salesperson例子中﹐父类别salesperson.bonus()和子类别salesmanager.bonus()皆表示同一涵义──求算红利﹐只是计算方法不同罢了。因之﹐「重新定义」着眼于以不同的执行过程来取代父类别之程序﹐但新旧程序之间﹐具有一致的意图。盼您能区分这易于混淆的概念。请再看个程序:
'ex02.bas
imports system.componentmodel
imports system.drawing
imports 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 overridable sub display()
messagebox.show("name: " + name + " age: " + str(age))
end sub
end class
public 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 overrides sub display()
mybase.display()
messagebox.show("salary: " + str(salary))
end sub
end class
public 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 overrides sub display()
mybase.display()
messagebox.show("studno: " + str(student_number))
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 teacher("linda", 33, 50500)
dim y as new student("tom", 36, 11138)
x.display()
y.display()
end sub
end class
此程序输出:
name: linda age: 33
salary: 50500
name: tom age: 36
studno: 11138
person的display()不能显示出teacher的salary资料,所以teacher必须「修正」person原有的display()程序。同样地,student类别也「修正」person原有的display()程序。n