2010-05-05 41 views
6

我不太清楚爲什麼或何時使用接口。有人可以在控制檯應用程序中使用VB.NET發佈完整,簡單和小型的界面示例。它如何可擴展?VB.NET接口

+2

可能的重複http://stackoverflow.com/questions/1983962/how-do-interfaces-work-and-how-to-use-them-in-practical-programming – 2010-05-05 00:34:34

+0

或http:// stackoverflow。 com/questions/122883/interfaces-why-can-i-scott-to-grasp-them – Walter 2010-05-05 12:24:46

+0

我建議你閱讀Juval Lowy的「.NET編程組件」;這是一本用於解釋許多不同重要概念的完整書籍,並且接口已被很好地涵蓋。 – STW 2010-05-05 19:11:53

回答

29

簡而言之:恩惠組成了繼承

接口僅僅是一套通用的,你想一個或多個類,以支持成員定義。關鍵是您必須在實現接口時明確提供功能。

您可以使用繼承實現類似的結果,因爲兩個子類可以從基礎繼承全功能成員。但是,繼承的缺點是你的子類最終會對基類產生嚴重依賴。

考慮下面的類:

Public Class Car 
    Publc Sub OpenDoor(ByVal key As MyKey) 
     Console.WriteLine("Access granted to car.") 
    End Sub 
End Class 

Public Class House 
    Public Sub OpenDoor(ByVal key as MyKey) 
     Console.WriteLine("Access granted to house.") 
    End Sub 
End Class 

你可以說,這兩個類是有點相關,因爲它們都有一個執行Opendoor()方法。您可能會試圖創建一個基類來提取常用功能。

Public Class OpenableProperty 
    Public Sub OpenDoor(ByVal key As MyKey) 
     Console.WriteLine("Access granted to property.") 
    End Sub 
End Class 

Public Class Car 
    Inherits OpenableProperty 
End Class 

Public Class House 
    Inherits OpenableProperty 
End Class 

然後,您可以用這種抽象是這樣的:

Public Class SecurityService 
    Public Sub InspectProperty(ByVal item As OpenableProperty) 
     Dim key As New MyKey() 
     Console.WriteLine("Inspecting property...") 
     item.OpenDoor(key) 
    End Sub 
End Class 

但是,與房子完全的事實,你可以一鍵訪問它們基於汽車是一個相當薄弱的抽象。哎呀,即使是一罐豆子也可以打開!

但還有其他一些關係可能會發生的點。例如,汽車和房屋都可能有空調:

Public Class Car 
    Inherits OpenableProperty 
    Public Sub TurnOnAirConditioning() 
     Console.WriteLine("Cool breezes flowing in car!") 
    End Sub 
End Class 

Public Class House 
    Inherits OpenableProperty 
    Public Sub TurnOnAirConditioning() 
     Console.WriteLine("Cool breezes flowing in house!") 
    End Sub 
End Class 

TurnOnAirConditioning()是否應該提取到基類?它與做一個OpenableProperty有什麼關係?如果沒有AC,JewelrySafe類可以繼承OpenableProperty嗎?在這種情況下,更好的答案是提取接口,並利用這些構成我們的類的功能,而不是繼承:

Public Interface IOpenable 
    Sub OpenDoor(ByVal key As MyKey) 
End Interface 

Public Interface ICoolable 
    Sub TurnOnAirConditioning() 
End Interface 

Public Class Car 
    Implements IOpenable, ICoolable 
    Public Sub OpenDoor(ByVal key as MyKey) Implements IOpenable.OpenDoor() 
     Console.WriteLine("Access granted to car.") 
    End Sub 
    Public Sub TurnOnAirConditioning() Implements ICoolable.TurnOnAirConditioning() 
     Console.WriteLine("Cool breezes flowing in car!") 
    End Sub 
End Class 

Public Class House 
    Implements IOpenable, ICoolable 
    Public Sub OpenDoor(ByVal key as MyKey) Implements IOpenable.OpenDoor() 
     Console.WriteLine("Access granted to house.") 
    End Sub 
    Public Sub TurnOnAirConditioning() Implements ICoolable.TurnOnAirConditioning() 
     Console.WriteLine("Cool breezes flowing in house!") 
    End Sub 
End Class 

Public Class JewelrySafe 
    Implements IOpenable 
    Public Sub OpenDoor(ByVal key as MyKey) Implements IOpenable.OpenDoor() 
     Console.WriteLine("Access granted to jewelry safe.") 
    End Sub 
End Class 

那麼你的抽象可以食用這樣:

Public Class SecurityService 
    Public Sub InspectProperty(ByVal item As IOpenable) 
     Dim key As New MyKey() 
     Console.WriteLine("Inspecting property...") 
     item.OpenDoor(key) 
    End Sub 
End Class 

Public Class ThermostatService 
    Public Sub TestAirConditioning(ByVal item as ICoolable) 
     Console.WriteLine("Testing Air Conditioning...") 
     item.TurnOnAirConditioning() 
    End Sub 
End Class 

的SecurityService可能然後用於檢查汽車,房屋和珠寶安全,而恆溫器服務僅可用於測試汽車和房屋的交流電。

Sub Main() 
    Dim securityService As New SecurityService() 
    Dim thermostatService As New ThermostatService() 

    Dim house As New House() 
    Dim car As New Car() 
    Dim jewelrySafe As New JewelrySafe() 

    With securityService 
     .InspectProperty(house) 
     .InspectProperty(car) 
     .InspectProperty(jewelrySafe) 
    End With 

    With thermostatService 
     .TestAirConditioning(house) 
     .TestAirConditioning(car) 
    End With 
End Sub 

這應該產生以下結果:

Inspecting property... 
Access granted to house. 
Inspecting property... 
Access granted to car. 
Inspecting property... 
Access granted to jewelry safe. 
Testing Air Conditioning... 
Cool breezes flowing in house! 
Testing Air Conditioning... 
Cool breezes flowing in car! 
+0

A *非常好看,易於理解並且非常明智的例子。獎勵!你是老師還是其他人? :) – pepoluan 2011-06-03 01:20:27

+0

不,只是在戰壕中的其他人偶爾會伸出援助之手。我想我在寫這本書的時候正在閱讀一些Head First書籍,並且他們的一些風格被擦掉了! – Technobabble 2011-06-17 21:50:53

+0

使用實現vs繼承的優秀解釋。一個評論,刪除()在顯式實現的末尾。例如,實現IOpenable.OpenDoor()應該讀取Implements IOpenable.OpenDoor – 2011-07-26 02:07:48

2

考慮一個簡單的界面:

Public Interface IWeightedValue 
    Public ReadOnly Property Weight As Double 
    Public ReadOnly Property Value As Double 
End Interface 

甚至沒有寫任何代碼,我就可以開始處理這個概念在其他地區我的代碼。例如:

Public Function GetWeightedAverage(ByVal points As IEnumerable(Of IWeightedValue)) As Double 
    Dim totalWeight As Double = 0.0 
    Dim totalWeightedValue As Double = 0.0 

    For Each point As IWeightedValue in points 
     totalWeight += point.Weight 
     totalWeightedValue += (point.Weight * point.Value) 
    Next 

    Return totalWeightedValue/totalWeight 
End Function 

瞧 - 現在爲我寫的任何一類,如果我只是讓它實現IWeightedValue那麼我可以計算出加權平均這個類的實例的集合。

+0

我覺得我幾乎可以理解接口的目的,但它仍然沒有完全點擊我。也許我應該在幾天後再回來再讀一遍。如果你給我另一個例子或者以某種方式闡述,我會給這個+1。 – Lopsided 2013-09-15 02:32:34