0

I'm trying to figure out this weird behavior. I have a base class (A) with an overloaded method called "M": one for integers, and one for floats (single on VB.NET).

On the other hand, I have a second class (B) that inherits from A, and it overloads method M in two ways: one for data type double, and one for object data type.

The problem is: I expect the methods to use each function for its data type, but for some strange reason, the method with Object data type takes all the calls. Why is that happening?

I expected the output of this program to be 15, but instead, it's 4.

Here's the code (VB.NET):

Module Module1

Public Class A

    Public Function M(ByVal a As Integer) As Integer
        Return 8
    End Function

    Public Function M(ByVal a As Single) As Integer
        Return 4
    End Function

End Class

Public Class B
    Inherits A

    Public Overloads Function M(ByVal a As Double) As Integer
        Return 2
    End Function

    Public Overloads Function M(ByVal a As Object) As Integer
        Return 1
    End Function

End Class


Sub Main()

    Dim a0 As Double = 1
    Dim a1 As Single = 2
    Dim a2 As Integer = 4
    Dim a3 As Object = 8

    Dim arre(4)

    arre(0) = a0
    arre(1) = a1
    arre(2) = a2
    arre(3) = a3

    Dim b As New B

    Dim suma% = 0

    For i = 0 To 3
        suma += b.M(arre(i))
    Next i

    Console.WriteLine(suma)

    System.Threading.Thread.CurrentThread.Sleep(2000)
End Sub

End Module
4

2 回答 2

1

The reason why it calls the overload that takes an Object is because your array is of type Object.

' original code
Dim arre(4)

arre(0) = a0
arre(1) = a1
arre(2) = a2
arre(3) = a3

' verbose version
Dim arre(4) As Object

arre(0) = DirectCast(a0, Object)
arre(1) = DirectCast(a1, Object)
arre(2) = DirectCast(a2, Object)
arre(3) = DirectCast(a3, Object)

Look up "boxing" and "unboxing" in .NET, there are tons of good articles that explain this in more detail.

于 2013-01-31T00:25:04.910 回答
0

The Object overload is being called in your example every time because you are declaring the array as a Object array. (Option Strict will force you to put As Object after it, but that doesn't change things.) It doesn't matter what type you put inside the array because the values are getting "boxed" into Objects

Consider the following, simplified to demonstrate what is happening (There is no need for your custom classes or inheritance to demonstrate what you saw)

Module Module1

Public Function M(ByVal a As Integer) As Integer
    Return 8
End Function

Public Function M(ByVal a As Object) As Integer
    Return 1
End Function

Sub Main()
    Dim a0 As Integer = 0
    Dim a1 As Object = 0
    Dim arre(1)
    arre(0) = a0
    arre(1) = a1

    Console.WriteLine(M(arre(0)))
    ' 1, arre() is implicitly typed as an array of Objects
    ' so the Object overload will be called

    Console.WriteLine(M(arre(1)))
    ' 1, see above

    Console.WriteLine(M(DirectCast(arre(0), Integer)))
    ' 8, explicit cast to an Integer

    Console.WriteLine(M(DirectCast(arre(1), Integer)))
    ' 8, see above

    Console.WriteLine(M(a0))
    ' 8, a0 declared explicitly as an Integer

    Console.WriteLine(M(a1))
    ' 1, a1 declared explicitly as an Object

    Threading.Thread.Sleep(2000)
End Sub

End Module
于 2013-01-31T00:37:24.510 回答