使用子类实现接口 [英] Implementing an Interface with Child Classes
问题描述
我有以下接口:
Interface IViewModel
...
End Interface
Interface ISpecialViewModel
Inherits IViewModel
...
End Interface
Interface IView
WriteOnly Property MyViewModel As IViewModel
End Interface
以下是我的课程:
Class VerySpecialViewModel
implements ISpecialViewModel
...
End Class
Class View
Implements IView
Public WriteOnly Property MyViewModel As VerySpecialViewModel Implements IView.MyViewModel
...
End Property
End Class
它告诉我'MyViewModel'无法实现'MyViewModel',因为接口'IView'上没有匹配属性。
It tells me that 'MyViewModel' cannot implement 'MyViewModel' because there is no matching property on interface 'IView'.
推荐答案
Public Interface ISomething
WriteOnly Property Prop As IParent
End Interface
您的类实现不满足该接口声明。请考虑以下情况:
That interface declaration isn't satisfied by your class implementation. Consider following situation:
还有另一个名为IChild2的接口:
There is another interface called IChild2:
Public Interface IChild2
Inherits IParent
...
End Interface
根据 ISomething
界面,您应该能够将实现 IChild2
的类的实例分配到 Thing .Prop
,因为它继承了 IParent
。但你不能,因为 Thing.Prop
属性 IChild
类型和 IChild2
不继承 IChild
According to ISomething
interface you should be able to assign instance of class implementing IChild2
into Thing.Prop
, because it inherits IParent
.But you can't, because Thing.Prop
property is of IChild
type and IChild2
does not inherits IChild
更新
Update
该解决方案怎么样? :
What about that solution:
Class ThingBase
Implements ISomething
Public WriteOnly Property Prop As IParent Implements ISomething.Prop
Set(value As IParent)
End Set
End Property
End Class
Class Thing
Inherits ThingBase
Public Overloads WriteOnly Property Prop As IChild
Set(value As IChild)
MyBase.Prop = value
End Set
End Property
End Class
Update2
Update2
Interface IView(Of T As IViewModel)
WriteOnly Property MyViewModel As T
End Interface
Class VerySpecialViewModel
Implements ISpecialViewModel
End Class
Class View
Implements IView(Of ISpecialViewModel)
Public WriteOnly Property MyViewModel As ISpecialViewModel Implements IView(Of ISpecialViewModel).MyViewModel
Set(value As ISpecialViewModel)
End Set
End Property
End Class
或
Class View
Implements IView(Of VerySpecialViewModel)
Public WriteOnly Property MyViewModel As VerySpecialViewModel Implements IView(Of VerySpecialViewModel).MyViewModel
Set(value As VerySpecialViewModel)
End Set
End Property
End Class
这篇关于使用子类实现接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!