VB.NET中的接口继承:我在实现中引用了哪一个?

时间:2017-04-13 14:49:58

标签: vb.net inheritance interface

我在Visual Studio 2015中使用Visual Basic .NET,我想知道以下内容。假设我有两个名为InterfaceAInterfaceB的接口,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继承属性InterfaceAA中的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

我更喜欢哪一个?这两者之间有什么区别,还是没关系?如果两者相同,我应该遵循哪些惯例或最佳实践?

1 个答案:

答案 0 :(得分:1)

它是Public Property A As String Implements InterfaceB.A,因为你要实现在InterfaceBImplementer类上强制转换的intefrace。