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

worldblog發表於2007-12-06
VB.Net中文教程(9) 重新定義(Overriding)程式 (轉)[@more@]

請注意 ......
著作權所有人:物澤事業股份有限公司、
  MISOO技術顧問團隊、物件導向雜誌作者、等。
u本摘自 物件導向雜誌、精通物件觀念與技術等書籍著作。
u本檔案僅供您的參閱,請遵守著作權法,不得做其它商業用途。


  主題:  重新定義(Overriding)


??????????  內容  ??????????
v 1. 重新定義程式

 

 

 


1. 重新定義(Overr)程式

  在應用上﹐常見如下之情況﹕子類別從父類別繼承之程式﹐並不合乎子類別之需要。此時可設計新程式取代之。

 
  圖1、程式成員之重新定義

  例如﹐SalesPerson類別含有Bonus()程式﹔SalesManager由 SalesPerson繼承而得Bonus()程式。Bonus()能計算銷售人員之紅利。然而﹐一般銷售員與銷售經理之紅利計算方法不同﹔所以﹐SalesPerson之Bonus()顯然並不適用於SalesManager。這SalesManager類別必須定義適用的Bonus()求算銷售經理之紅利。子類別不滿意繼承之程式而自行定義程式取代之﹐此情形稱為「再定義」(Redefinition)或「重新定義」(Overriding)。自行定義程式與繼承程式之名稱相同。請看個程式﹕

'ex01.bas
Imports System.ComponentModel
Imports System.Drawing
Imports System.s
'-----------------------------------------------------------------------------------------
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 FoDesigner.
  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 " Form Designer generated code "
  .......
#End Region
  Protected Sub Form1_Click( ByVal sender As , 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


 


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-988986/,如需轉載,請註明出處,否則將追究法律責任。

相關文章