【发布时间】:2010-12-19 13:55:13
【问题描述】:
我有一个复制函数,我想在子类中重写它以返回子类的类型。这是我的界面:
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
这是我的课程。如您所见,当 Car 覆盖 Copy 它返回 ICar 这就是我想要的。但是当 Toyota 覆盖 Copy 时,它还希望返回 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
【问题讨论】:
标签: .net vb.net generics inheritance interface