如果我具有以下条件:
Class TestClass(Of T As {New, BaseClass})
Private Instance As T
Public Sub New(Instance As T)
Me.Instance = Instance
End Sub
Function GetSomething() As String
Return Instance.GetSomething()
End Function
End Class
Class BaseClass
Sub New()
' Do something
End Sub
Overridable Function GetSomething() As String
Return "Hello"
End Function
End Class
Class DerivedClass
Inherits BaseClass
Public Overrides Function GetSomething() As String
Return MyBase.GetSomething() & " Bob"
End Function
End Class
Class PrinterOfHello
Private HelloGetter As TestClass(Of BaseClass)
Sub New()
HelloGetter = New TestClass(Of DerivedClass)(New DerivedClass)
Debug.Print(HelloGetter.GetSomething)
End Sub
End Class
我为什么不能这样做?
类型'TestClass(Of DerivedClass)'的值不能转换为'TestClass(Of BaseClass)'
您可以放心,无论您尝试分配给 HelloGetter 哪种类型,都确实可以实现 GetSomething 。您可以将DerivedClass分配给BaseClass变量,那么为什么不能使用泛型呢?
这是真实的信息:
类型'Bot(Of TConfig As {RoomConfigBase,New})'的值不能转换为'Bot(Of RoomConfigBase)'
简而言之,只要它实现了RoomConfigBase实现的方法,我都不关心TConfig中还有什么其他逻辑。我是否必须使用界面?