level 10
liqiochi
楼主
最近自学vb.net时使用的《Professional Visual Basic 2012 and .NET 4.5 Programming》
一上书看到如下代码:(我把不相关的代码省略了)
Module Sort
Public Delegate Function Compare(ByVal v1 As Object, ByVal v2 As Object) As Boolean
Public Sub DoSort(ByVal theData() As Object, ByVal GreaterThan As Compare)
Dim outer As Integer
Dim inner As Integer
Dim temp As Object
For outer = 0 To UBound(theData)
For inner = outer + 1 To UBound(theData)
If GreaterThan.Invoke(theData(outer), theData(inner)) Then
temp = theData(outer)
theData(outer) = theData(inner)
theData(inner) = temp
End If
Next
Next
End Sub
End Module
Public Class Person
Public Shared Function CompareAge(ByVal Person1 As Person, _
ByVal Person2 As Person) As Boolean
Return Person1.Age > Person2.Age
End Function
Public Shared Function CompareAge(ByVal Person1 As Object, _
ByVal Person2 As Object) As Boolean
Return CType(Person1, Person).Age > CType(Person2, Person).Age
End Function
Public Shared Function CompareName(ByVal Person1 As Object, _
ByVal Person2 As Object) As Boolean
Return CType(Person1, Person).Name > CType(Person2, Person).Name
End Function
End Class
Public Class Form1
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim myPeople(4) As Person
myPeople(0) = New Person("Fred",
#7/9/1960#
)
myPeople(1) = New Person("Mary",
#1/21/1955#
)
myPeople(2) = New Person("Sarah",
#2/1/1960#
)
myPeople(3) = New Person("George",
#5/13/1970#
)
myPeople(4) = New Person("Andre",
#10/1/1965#
)
DoSort(myPeople, AddressOf Person.CompareName)
End Sub
End Class
在sort模块中的dosort过程里,传递的参数是object类型的,但是实际使用时,传递的myPeople的类型却是Person类型,CompareName这个函数也要求要object类型的参数,这种签名不一致的情况为什么可以运行?
2016年12月29日 07点12分
1
一上书看到如下代码:(我把不相关的代码省略了)
Module Sort
Public Delegate Function Compare(ByVal v1 As Object, ByVal v2 As Object) As Boolean
Public Sub DoSort(ByVal theData() As Object, ByVal GreaterThan As Compare)
Dim outer As Integer
Dim inner As Integer
Dim temp As Object
For outer = 0 To UBound(theData)
For inner = outer + 1 To UBound(theData)
If GreaterThan.Invoke(theData(outer), theData(inner)) Then
temp = theData(outer)
theData(outer) = theData(inner)
theData(inner) = temp
End If
Next
Next
End Sub
End Module
Public Class Person
Public Shared Function CompareAge(ByVal Person1 As Person, _
ByVal Person2 As Person) As Boolean
Return Person1.Age > Person2.Age
End Function
Public Shared Function CompareAge(ByVal Person1 As Object, _
ByVal Person2 As Object) As Boolean
Return CType(Person1, Person).Age > CType(Person2, Person).Age
End Function
Public Shared Function CompareName(ByVal Person1 As Object, _
ByVal Person2 As Object) As Boolean
Return CType(Person1, Person).Name > CType(Person2, Person).Name
End Function
End Class
Public Class Form1
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim myPeople(4) As Person
myPeople(0) = New Person("Fred",
#7/9/1960#
)
myPeople(1) = New Person("Mary",
#1/21/1955#
)
myPeople(2) = New Person("Sarah",
#2/1/1960#
)
myPeople(3) = New Person("George",
#5/13/1970#
)
myPeople(4) = New Person("Andre",
#10/1/1965#
)
DoSort(myPeople, AddressOf Person.CompareName)
End Sub
End Class
在sort模块中的dosort过程里,传递的参数是object类型的,但是实际使用时,传递的myPeople的类型却是Person类型,CompareName这个函数也要求要object类型的参数,这种签名不一致的情况为什么可以运行?