8

考虑以下代码:

Public Class Animal

Public Overridable Function Speak() As String
    Return "Hello"
End Function

End Class

Public Class Dog
    Inherits Animal

    Public Overrides Function Speak() As String
        Return "Ruff"
    End Function

End Class

Dim dog As New Dog
Dim animal As Animal
animal = CType(dog, Animal)
// Want "Hello", getting "Ruff"
animal.Speak()

如何将 Dog 的实例转换/ctype 为 Animal 并调用 Animal.Speak?

4

5 回答 5

13

你没有;根据继承的定义,子类的方法会覆盖超类的方法。

如果您希望覆盖的方法可用,请将其公开在子类中,例如

Public Class Dog 
    Inherits Animal
    Public Overrides Function Speak() As String
        Return "Ruff"
    End Function
    Public Function SpeakAsAnimal() As String
        Return MyBase.Speak()
    End Function
End Class
于 2008-10-28T21:21:48.177 回答
1

I would ask why you are trying to get this type of behavior. It seems to me that the fact you need to invoke the parent class' implementation of a method is an indication that you have a design flaw somewhere else in the system.

Bottom line though, as others have stated there is no way to invoke the parent class' implementation given the way you've structured your classes. Now within the Dog class you could call

MyBase.Speak()

which would invoke the parent class' implementation, but from outside the Dog class there's no way to do it.

于 2008-10-28T22:01:10.923 回答
0

我认为如果你放弃“Overridable”并将“Overrides”更改为“New”,你会得到你想要的。

Public Class Animal

Public Function Speak() As String
    Return "Hello"
End Function

End Class

Public Class Dog
    Inherits Animal

    Public New Function Speak() As String
        Return "Ruff"
    End Function

End Class

Dim dog As New Dog
Dim animal As Animal
dog.Speak() ' should be "Ruff"
animal = CType(dog, Animal)
animal.Speak() ' should be "Hello"
于 2008-11-26T15:33:18.610 回答
0

我不认为你可以。

问题是对象仍然是一只狗。您所描述的行为(从被投射的对象中获取“ruff”而不是“hello”)是标准的,因为您希望能够使用动物类让一群不同类型的动物说话。

例如,如果您有这样的第三类:

Public Class Cat
    Inherits Animal

    Public Overrides Function Speak() As String
        Return "Meow"
    End Function
End Class

然后你就可以像这样访问它们:

protected sub Something
    Dim oCat as New Cat
    Dim oDog as New Dog

    MakeSpeak(oCat)
    MakeSpeak(oDog)
End sub

protected sub MakeSpeak(ani as animal)
    Console.WriteLine(ani.Speak())
end sub 

你所说的基本上打破了继承链。现在,这可以通过设置Speak函数来接受一个参数来完成,该参数告诉它是否返回它的基值或基值的单独 SPEAK 函数,但开箱即用,你不会得到以这种方式表现的事物。

于 2008-10-28T21:23:53.273 回答
0

我知道这是几个月前发布的,但我仍然会尝试回复,也许只是为了完整起见。

有人告诉您,您可以从类中访问被覆盖的方法然后dog可以用不同的名称公开它。但是使用条件呢?

你可以这样做:

Public Class Animal

Public Overridable Function Speak(Optional ByVal speakNormal as Boolean = False) As String
    Return "Hello"
End Function

End Class

Public Class Dog
    Inherits Animal

    Public Overrides Function Speak(Optional ByVal speakNormal as Boolean = False) As String
        If speakNormal then
            return MyBase.Speak()
        Else
            Return "Ruff"
        End If
    End Function

End Class

然后像这样称呼他们:

Dim dog As New Dog
Dim animal As new Animal
animal.Speak() //"Hello"
dog.Speak()//"Ruff"
dog.Speak(true)//"Hello"

或者,您可以getTheAnimalInTheDog制作 Speak()

你可以这样做:

Public Class Animal

Public Overridable Function Speak() As String
    Return "Hello"
End Function

Public MustOverride Function GetTheAnimalInMe() As Animal

End Class

Public Class Dog
    Inherits Animal

    Public Overrides Function Speak() As String
        Return "Ruff"
    End Function

    Public Overrides Function GetTheAnimalInMe() As Animal
        Dim a As New Animal
        //Load a with the necessary custom parameters (if any)
        Return a
    End Function
End Class

然后再一次:

Dim dog As New Dog
Dim animal As new Animal
animal.Speak() //"Hello"
dog.Speak()//"Ruff"
dog.GetTheAnimalInMe().Speak()//"Hello"

希望能帮助到你 ;)

于 2014-12-22T08:34:06.797 回答