VB.Net中文教程(5)程式多重定義 (轉)
請注意 ......
著作權所有人:物澤事業股份有限公司、
MISOO技術顧問團隊、物件導向雜誌作者、等。
u本摘自 物件導向雜誌、精通物件觀念與技術等書籍著作。
u本檔案僅供您的參閱,請遵守著作權法,不得做其它商業用途。
主題: 多重定義(Overloading)
?????? 內容 ??????
v 1. 程式成員的多重定義
1. 程式成員的多重定義
「程式多重定義」(Function Overloading)又稱為「程式重複定義」。它讓物件更具彈性﹐能處理多樣化之訊息。這觀念源於日常生活。例如﹐我們常說﹕
◎ 貓 玩 繡球
◎ 貓 玩 老鼠
◎ 貓 玩 魚
貓玩繡球與玩老鼠之玩法不盡相同。但何以使用同一動詞──「玩」呢﹖也許人們認為其目的是一致的﹕貓獲得快樂。上述的「貓」為類別﹐而某隻貓是物件。例如﹕加菲貓是物件﹐它可接受訊息──
其中﹐「玩」代表著動作和過程﹐而繡球、老鼠及魚則是「玩」之物件。回想﹐在程式中﹐「程式」代表一項動作及過程﹐而「自變數值」則為程式之處理物件。因之﹐上圖可表示為──
圖1、 play()之多重定義
之理想為﹕讓程式之寫法與人們日常生活經驗吻合﹐於是設計個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.bas
Imports System.ComponentModel
Imports System.Drawing
Imports System.s
'-------------------------------------------------------
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 Sub
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 overrs 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 a As New Example()
a.Display()
a.Display("Taiwan")
a.Display(80)
a.Display(100.5)
End Sub
End Class
此程式輸出如下﹕ ****
Taiwan
80
102.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.bas
Imports System.ComponentModel
Imports System.Drawing
Imports 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 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 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 Sub
End Class
此程式輸出如下﹕ Sum = 8
Sum = 85
Sum = 127
當計算機執行到指令── b.Add( 100, 27 ),由於有兩個自變數﹐且型態皆為Integer﹔計算機就選上並執行第三個Add() 程式。此時計算機把100傳給x﹐而27傳給y。這多重定義之觀念﹐也常用於建構者程式上。例如﹕
'ex03.bas
Imports System.ComponentModel
Imports System.Drawing
Imports 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 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 r1 As New Rectangle()
Dim r2 As New Rectangle(8)
Dim r3 As New Rectangle(5, 6)
r1.ShowArea()
r2.ShowArea()
r3.ShowArea()
End Sub
End 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.bas
Imports System.ComponentModel
Imports System.Drawing
Imports 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 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 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 Sub
End 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.bas
Imports System.ComponentModel
Imports System.Drawing
Imports 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 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 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 Sub
End Class
此程式輸出﹕
NOT EQUAL
EQUAL
EQUAL
由於各程式名稱不相同,您就得記憶各程式之名字﹐徒增記憶負擔而且易於犯錯﹐並不合乎人們生活習慣。因之﹐VB的多重定義觀念﹐能增加程式之彈性及親切感。
程式多重定義情形並不限於單一類別之內,也可以發生於父子類別之間。例如:
'ex06.bas
Imports System.ComponentModel
Imports System.Drawing
Imports 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 Sub
End Class
Public 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 Sub
End Class
Public 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 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 Person()
x.SetValue("Alvin", 32)
Dim y As New Student()
y.SetValue("Tom", 36, 11138)
x.Display()
y.pr()
End Sub
End Class
Teacher類別從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.bas
Imports System.ComponentModel
Imports System.Drawing
Imports 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 Sub
End Class
Public 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 Sub
End Class
Public 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 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 Person()
x.SetValue("Alvin", 32)
Dim y As New Student()
y.SetValue("Tom", 36, 11138)
x.Display()
y.Display()
End Sub
End 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
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-988979/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 程式的定義
- 好程式設計師Java教程系列之異常定義程式設計師Java
- gin自定義驗證器&轉化中文
- 微軟程式碼編輯器Visual Studio Code Mac中文設定教程微軟Mac
- React轉微信小程式:從React類定義到Component呼叫React微信小程式
- 城市天際線中文在哪裡設定?城市天際線中文設定教程
- 在SSIS中自定義VB.Net指令碼(下)OF指令碼
- pytest教程之多重校驗
- 人人都能學會的python程式設計教程11:定義函式Python程式設計函式
- VARCHART XGantt系列教程:使用顏色來定義語義
- C#/VB.NET 將RTF轉為HTMLC#HTML
- C#/VB.NET 將Html轉為ExcelC#HTMLExcel
- macOS自定義定時開關機教程Mac
- telegram電報怎麼設定中文教程
- C#/VB.NET 將彩色PDF轉為灰度C#
- 1.2程式設計基礎之變數定義、賦值及轉換程式設計變數賦值
- Final Cut Pro中文新手教程(六三)自定義視訊封面
- C++入門教程(12):定義函式C++函式
- 「Premiere中文新手教程」音訊設定與調節REM音訊
- Python多重繼承注意事項!Python教程Python繼承
- Python的多重繼承如何使用?python教程Python繼承
- 音樂播放器Spotify Mac版中文mian費版中文設定教程播放器Mac
- Ubuntu系統設定中文語言的方法教程,Ubuntu系統怎麼設定中文語言?Ubuntu
- SAPUI5教程——最全中文學習指南(必看)UI
- 兄弟連go教程(11)函式 - 函式定義Go函式
- Python私有變數如何定義?Python學習教程!Python變數
- mysql 報錯json字串 中文漢字轉義的問題MySqlJSON字串
- SAP UI5和angular裡的常量定義UIAngular
- 簡單-定義一個小程式元件元件
- rubymine設定自定義快捷程式碼片段
- Dreamweaver使用img標籤定義影像高寬的教程
- XamarinAndroid元件教程設定自定義子元素動畫(二)NaNAndroid元件動畫
- XamarinAndroid元件教程設定自定義子元素動畫(一)NaNAndroid元件動畫
- 物聯網學習教程—用typedef定義型別型別
- C#/VB.NET 實現Word和ODT文件相互轉換C#
- 小程式轉換工具—Antmove 教程解析
- Spring Data JPA系列5:讓IDEA自動幫你寫JPA實體定義程式碼SpringIdea
- 座標系定義和相互轉換演算法演算法
- OPC Server示例教程:VC#和VB.NET中的簡單APIServerAPI