手机站
网通分站
电信主站
密 码:
用户名:
当前位置 : 主页>网络编程>Asp.Net编程>列表

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

来源:互联网 作者:西部数码 时间:2008-04-10
西部数码-全国虚拟主机10强!40余项虚拟主机管理功能,全国领先!双线多线虚拟主机南北访问畅通无阻!免费赠送企业邮局,.CN域名,自助建站480元起,免费试用7天,满意再付款! P4主机租用799元/月.月付免压金!

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))

文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!