VB.NET接口

时间:2010-05-05 00:05:17

标签: asp.net vb.net class interface

我不清楚为什么或何时使用Interfaces。有人可以在控制台应用程序中使用VB.NET发布完整,简单和小型的接口示例。它是如何可扩展的?

2 个答案:

答案 0 :(得分: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有什么关系? JewelrySafe类可以在没有AC的情况下从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检查Car,House和JewelrySafe,而ThermostatService只能用于测试Car and House的AC。

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!

答案 1 :(得分: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,那么我可以计算这个类的实例集合的加权平均值