我在Visual Studio 2015中使用Visual Basic .NET,我想知道以下内容。假设我有两个名为InterfaceA
和InterfaceB
的接口,InterfaceB
继承自InterfaceA
,如下所示:
Public Interface InterfaceA
Property A As String ' Type not important here.
End Interface
Public Interface InterfaceB
Inherits InterfaceA
Property B As Integer ' Type not important here, either.
End Interface
现在我要创建一个名为InterfaceBImplementer
的类,(顾名思义)实现InterfaceB
:
Public Class InterfaceBImplementer
Implements InterfaceB
Public Property B As Integer Implements InterfaceB.B
Get
' Snip.
End Get
Set(value As Integer)
' Snip.
End Set
End Property
' To be continued...
到目前为止,这么好。财产B
只能在InterfaceB
中找到,因此不存在问题来源。
但是,InterfaceB
会从A
继承属性InterfaceA
。 A
中的InterfaceBImplementer
的以下两种实现似乎都是有效的:
' Referring to InterfaceA.
Public Property A As String Implements InterfaceA.A
Get
' Snip.
End Get
Set(value As String)
' Snip.
End Set
End Property
或:
' Referring to InterfaceB.
Public Property A As String Implements InterfaceB.A
Get
' Snip.
End Get
Set(value As String)
' Snip.
End Set
End Property
我更喜欢哪一个?这两者之间有什么区别,还是没关系?如果两者相同,我应该遵循哪些惯例或最佳实践?
答案 0 :(得分:1)
它是Public Property A As String Implements InterfaceB.A
,因为你要实现在InterfaceBImplementer
类上强制转换的intefrace。