跳至內容

面向物件程式設計/介面

來自華夏公益教科書

介面允許一個類根據上下文暴露不同的屬性和方法集。例如,假設您正在編寫一個管理客戶的 Visual Basic 2005 應用程式。客戶可以是個人,也可以是組織。程式的一部分生成客戶的地址標籤。

您可以這樣定義個人

Class Person
    Public LastName As String
    Public FirstName As String
    Public Address As String
End Class

您也可以這樣定義組織

Class Organization
    Public Name As String
    Public MailingAddress As String
    Public BuildingAddress As String
End Class

如您所見,個人和組織之間的主要區別在於

  • 個人既有姓又有名,而組織只有一個名稱
  • 組織有單獨的郵寄地址,而個人只有一個地址

為了使您可以使用同一個例程列印地址標籤,您建立了一個名為“IAddressLabel”的介面(按照慣例,介面的名稱應該以大寫字母 I 開頭)。

Interface IAddressLabel
    ReadOnly Property Name() As String
    ReadOnly Property Address() As String
End Interface

現在您修改 Person 類以實現此介面。主要需要注意的是,Person 的姓和名在標籤上合併在一起。

Class Person
    Implements IAddressLabel
    Public LastName As String
    Public FirstName As String
    Public Address As String
    Public ReadOnly Property Address1() As String Implements IAddressLabel.Address
        Get
            Return Address
        End Get
    End Property
    Public ReadOnly Property Name() As String Implements IAddressLabel.Name
        Get
            Return FirstName & " " & LastName 'combine the first and last names for the address label
        End Get
    End Property
End Class

現在您修改 Organization 類以實現 IAddressLabel 介面。請注意,標籤上的地址是郵寄地址,而不是建築地址。

Class Organization
    Implements IAddressLabel
    Public Name As String
    Public MailingAddress As String
    Public BuildingAddress As String
    Public ReadOnly Property Address() As String Implements IAddressLabel.Address
        Get
            Return MailingAddress 'Use the mailing address for the address label
        End Get
    End Property
    Public ReadOnly Property Name1() As String Implements IAddressLabel.Name
        Get
            Return Name
        End Get
    End Property
End Class

現在您可以將不同的物件組合在一起,形成一個地址標籤集合!

Sub Main()
    'create a person object
    Dim Bill As New Person
    Bill.FirstName = "William"
    Bill.LastName = "Jones"
    Bill.Address = "123 Test Rd Testville 9123"
    'create another person object
    Dim Sally As New Person
    Sally.FirstName = "Sally"
    Sally.LastName = "Smith"
    Sally.Address = "999 Sample Ave Testburg 9222"
    'create an Organization object
    Dim WidgetsInc As New Organization
    WidgetsInc.Name = "Widgets Incorporated"
    WidgetsInc.BuildingAddress = "9123 Avenue Rd Sampletown 9876"
    WidgetsInc.MailingAddress = "P.O. Box 123 Sampletown 9876"
    'Because all three objects implement the IAddressLabel interface, we can put them in the same array
    Dim MailingList(2) As IAddressLabel
    MailingList(0) = Bill
    MailingList(1) = Sally
    MailingList(2) = WidgetsInc
    'loop through, displaying the address label for each object
    Dim i As Integer
    For i = 0 To 2
        MsgBox(MailingList(i).Name & vbCrLf & MailingList(i).Address)
    Next i
End Sub

介面 vs. 繼承

[編輯 | 編輯原始碼]

介面和繼承都可以用來解決將不同物件集體處理的問題。例如,如果您有 Cat 類和 Dog 類,但有一些例程需要一起處理它們,您可以

  • 建立一個 Animal 基類,其中包含 Cat 和 Dog 共有的過程,並讓 Cat 和 Dog 繼承自 Animal,或者
  • 建立一個 IAnimal 介面,並讓 Cat 和 Dog 都實現該介面。

使用哪種方法取決於幾個因素。一般來說

  • 如果您要擴充套件現有類,則使用繼承。例如,如果您已經有了 Animal 類,然後發現需要區分 Cat 和 Dog
  • 如果您只是想將不同的物件視為相同,則使用介面。例如,您已經有了 Cat 和 Dog 類,然後發現需要以類似的方式操作它們

元件的引入

[編輯 | 編輯原始碼]

繼承 vs 委託

[編輯 | 編輯原始碼]

動態繼承

[編輯 | 編輯原始碼]
華夏公益教科書