VB.Net中文教程(9) 重新定義(Overriding)程式

來源:互聯網
上載者:User
程式|教程|中文 主題: 重新定義(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





相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。