VB.NET允许在同一个类中定义两个或多个同名方法。这些同名方法执行的代码不同,完成的功能也不同。方法的重载用关键字Overloads实现,比如:
Public Overloads Sub Okay()
Public Overloads Sub Okay(byval a As Integer)
Public OverLoads Sub Okay (Byval a As Integer, Byval b As Integer)
上述代码将存在同一个类中定义三个名为Okay的过程,它们的参数不同,执行的代码也不同。当通过类的对象实例调用Okay过程时,根据调用时的参数不同,对象自动选择执行与参数个数、类型等相符的过程。
下面的例子的函数签名有点乱
Module Module1
Public Class Point
Protected X As Single = 0, Y As Single = 0
Public Sub pSet(ByVal xPoint As Single, ByVal yPoint As Single)
X = xPoint
Y = yPoint
End Sub
Public Sub pSet(ByRef OldPoint As Point)
X = OldPoint.X
Y = OldPoint.Y
End Sub
Public Sub pSet(ByVal xyPoint As Single)
X = xyPoint
Y = xyPoint
End Sub
Public Function GetPoint(ByRef xPoint As Single, ByRef yPoint As Single) As Boolean
xPoint = X
yPoint = Y
Return True
End Function
Public Function GetPoint(ByRef thePoint As Point) As Boolean
thePoint.pSet(X, Y)
Return True
End Function
Public Function GetPoint() As Point
Dim NewPoint As New Point
NewPoint.pSet(X, Y)
Return NewPoint
End Function
End Class
Sub Main()
Dim oldPoint As New Point
Dim MyPoint As New Point
Dim temPoint As Point
Dim x As Single = 1000, y As Single = 2000
oldPoint.pSet(x, y)
MyPoint.pSet(oldPoint)
temPoint = MyPoint.GetPoint
temPoint.GetPoint(x, y)
Console.WriteLine("tempPoint的坐标是" & x & "," & y)
MyPoint.GetPoint(oldPoint)
oldPoint.GetPoint(x, y)
Console.WriteLine("OldPoint的坐标是" & x & "," & y)
Console.Read()
End Sub
End Module