gpt4 book ai didi

.net - 派生类的泛型版本可以使用泛型类型重写基方法吗?

转载 作者:行者123 更新时间:2023-12-02 18:00:43 25 4
gpt4 key购买 nike

考虑:

Public MustInherit Class Column
Public ReadOnly Property ReturnSomethingUseful() As Object
Get
'return something useful
End Get
End Property
End Class

Public Class TypedColumn(Of T)
Inherits Column

Public Overrides ReadOnly Property ReturnSomethingUseful() As T
Get
'return the same something useful, but as type T
Return MyBase.ReturnSomethingUseful()
End Get
End Property
End Class

但这会出现以下错误:

Public Overrides Function ReturnSomethingUseful(sValue As String) As Boolean' cannot override 'Public Overridable Function ReturnSomethingUseful(sValue As String) As Object' because they differ by their return types.

我承认你不能这样做,但我希望能够保留我尝试要做的事情的语义,即拥有一个处理与Object,但派生类中的类型化版本知道特定类型T。有什么办法可以实现这一点吗?

最佳答案

是的,有:使用 Shadows修改器

Public Shadows ReadOnly Property ReturnSomethingUseful() As T
Get
'return the same something useful, but as type T
Return MyBase.ReturnSomethingUseful()
End Get
End Property

关于.net - 派生类的泛型版本可以使用泛型类型重写基方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10141726/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com