接口&具有不同类型的子类

时间:2015-08-29 16:25:30

标签: vb.net

我有以下情况,我很确定可以通过使用接口来解决,但我没有足够的经验来实现并且不断失去自己。

Public Class IPGateway
  Property Hostname As String
  Property Port As Integer
  Public Function Read()
     Return OtherClass.Read(Hostname, Port)
  End Sub
End Class

Public Class SerialGateway
  Property Port As String
  Property Baud As Integer
  Public Sub Read()
    Return YetAnotherClass.Read(Port, Baud)
  End Sub
End Class


Public Sub Main()
  Dim GatewayList as New List(of Gateways?) <- Interface here?
  GatewayList.Add(SerialGateway)
  GatewayList.Add(IPGateway)
  For each gateway in GatewayList
   Console.Write(gateway.Hostname() OR gateway.Baud)
   Console.Write(gateway.Read())
  Next
End Sub

我欢迎就如何最好地实施这种方法提出建议?

1 个答案:

答案 0 :(得分:0)

您有2个选择,一个使用抽象类(MustInherit for VB),另一个使用接口。你必须决定哪一个最适合你!

Module Module1

Sub Main()
    Console.WriteLine("With Abstract Classes")

    Dim GatewayList As New List(Of TheBase)
    GatewayList.Add(New SerialGateway With {.Baud = 5, .Port = "10"})
    GatewayList.Add(New IPGateway With {.Hostname = "Test", .Port = "15"})
    For Each gateway In GatewayList
        Console.WriteLine(gateway.Port)
        If TypeOf gateway Is SerialGateway Then
            Console.WriteLine(DirectCast(gateway, SerialGateway).Baud)
        ElseIf TypeOf gateway Is IPGateway Then
            Console.WriteLine(DirectCast(gateway, IPGateway).Hostname)
        End If

        Console.WriteLine(gateway.Read())
    Next


    Console.WriteLine("With Interface")

    Dim GatewayList2 As New List(Of TestIn)
    GatewayList2.Add(New SerialGateway2 With {.Baud = "5", .Port = "10"})
    GatewayList2.Add(New IPGateway2 With {.Hostname = "Test", .Port = "15"})
    For Each gateway As TestIn In GatewayList2
        Console.WriteLine(gateway.Port)
        Console.WriteLine(gateway.Property2)

        Console.WriteLine(gateway.Read())
    Next

    Console.ReadLine()
End Sub

End Module

Public Class IPGateway
    Inherits TheBase

    Property Hostname As String

    Public Overrides Function Read() As String
        Return "IPGateway"
    End Function
End Class

Public Class SerialGateway
    Inherits TheBase
    Property Baud As Integer

    Public Overrides Function Read() As String
        Return "SerialGateway"
    End Function
End Class

Public MustInherit Class TheBase
    Public Property Port As String
    Public MustOverride Function Read() As String
End Class

Public Interface TestIn
    Property Port As String
    Property Property2 As String

    Function Read() As String
End Interface

Public Class SerialGateway2
    Implements TestIn

    Public Property Port As String Implements TestIn.Port
    Public Property Baud As String Implements TestIn.Property2

    Public Function Read() As String Implements TestIn.Read
        Return "SerialGateway2"
    End Function
End Class

Public Class IPGateway2
    Implements TestIn

    Public Property Port As String Implements TestIn.Port
    Public Property Hostname As String Implements TestIn.Property2

    Public Function Read() As String Implements TestIn.Read
        Return "IPGateway2"
    End Function
End Class