我有一个复制函数,我想在子类中重写以返回子类的类型。这里是我的接口:如何获得一个基类方法的返回类型是子类的类型?
Public Interface IBase(Of T)
Function Copy() As T
End Interface
Public Interface ICar
Inherits IBase(Of ICar)
End Interface
Public Interface IToyota
Inherits ICar
End Interface
这里是我的课程。正如你所看到的,当汽车覆盖复制它返回ICar这是我想要的。但是,当丰田覆盖复制它也想要返回ICar而不是IToyota。我如何编写它以返回IToyota?
Public MustInherit Class Base(Of T)
Implements IBase(Of T)
Protected MustOverride Function Copy() As T Implements IBase(Of T).Copy
End Class
Public Class Car
Inherits Base(Of ICar)
Implements ICar
Protected Overrides Function Copy() As ICar
Return Nothing //'TODO: Implement Copy
End Function
End Class
Public Class Toyota
Inherits Car
Implements IToyota
Protected Overrides Function Copy() As IToyota Implements IToyota.Copy
//'I want this to return IToyota, but gives me a compile error
End Function
End Class