VB如果基类实现接口,派生类是否也需要实现?

时间:2017-11-15 16:39:49

标签: vb.net oop

我正在分析代码。我们有一个实现接口的基类(不是抽象)。然后我们有一个基类的派生类,它也实现了接口 界面:

Public Interface MainInterface

    Function getMetaThreads(ByVal ThreadCount As Integer) As String()
    Property Name() As String
    Property TargetDirectory() As String
    Property TimeOut() As Long
    ReadOnly Property ExecutableAssembly() As String
    ReadOnly Property IsVersionValid() As Boolean

End Interface

基类:

Public Class BaseClass
    Implements MainInterface

    Public Sub print() 
        Console.WriteLine("BaseClass printing")
    End Sub
    Public ReadOnly Property ExecutableAssembly As String Implements MainInterface.ExecutableAssembly
        ...
    End Property

    Public ReadOnly Property IsVersionValid As Boolean Implements MainInterface.IsVersionValid
        ...
    End Property
    Protected strName As String = ""
    Protected strTargetDirectory As String = ""
    Protected lngTimeout As Long = 0

    Public Property Name As String Implements MainInterface.Name
        ...
    End Property

    Public Property TargetDirectory As String Implements MainInterface.TargetDirectory
        ...
    End Property

    Public Property TimeOut As Long Implements MainInterface.TimeOut
        ...
    End Property

    Public Function getMetaThreads(ThreadCount As Integer) As String() Implements MainInterface.getMetaThreads
        ...
    End Function
    Public  Overridable Function  myOwnFunc() As String
       Return ""
    End Function
End Class

派生类:

Public Class SubClass
    Inherits BaseClass
    Implements MainInterface

    Public Function getMetaThreads(ThreadCount As Integer) As String() Implements MainInterface.getMetaThreads
        Return myOwnFunc()
    End Function

    Overrides Function myOwnFunc() As String()
        Dim l As New List (Of String) 
        l.Add("44")
        l.Add("33")
        return l.ToArray()

    End Function
End Class

再次在派生类中实现接口是否有意义?有必要这样的理由或案例吗?我认为让基类实现接口应该足够了,并且在接口中实现它是多余的。

0 个答案:

没有答案