VB.NET接口 [英] VB.NET Interfaces

查看:274
本文介绍了VB.NET接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有,为什么或者何时使用接口相当清楚。有人可以张贴在控制台应用程序使用VB.NET接口的完整,简单的小例子。它是如何扩展?

I am not quite clear as to why or when to use Interfaces. Can someone post a complete, simple and small example of an Interface using VB.NET in a Console Application. How is it extensible?

推荐答案

在短期:飞亚组成了继承

In short: Favor Composition over Inheritance

接口仅仅是一套通用的,你想一个或多个类,以支持成员定义。关键是你有,当你实现一个接口来明确提供的功能。

Interfaces are simply a common set of member definitions that you want one or more classes to support. The key is that you have to provide the functionality explicitly when you implement an interface.

您可以使用继承自两个子类可以继承基本功能齐全的会员达到类似的效果。但继承的缺点是,你的子类最终不得不在基类的硬依赖关系。

You can achieve similar results using inheritance since two sub-classes can inherit fully-functional members from the base. But the downside of inheritance is that your sub-classes end up having a hard dependency on the base class.

考虑如下的类:

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()方法。你也许会,甚至创建一个基类中提取常见的功能。

You could say that these two classes are somewhat related because they both have an OpenDoor() method. You might be tempted to even create a base class to extract common functionality.

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

然后,您可以使用这种抽象是这样的:

You could then use this abstraction like this:

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

但是,与房子完全的事实,你可以一键访问它们基于车是pretty弱抽象。哎呀,甚至豆罐头可以打开的!

However, relating a house to a car based solely on the fact that you can access them with a key is a pretty weak abstraction. Heck, even a can of beans can be openable!

但也有关系的地方,可能会出现以及其他问题。例如,无论是汽车和房子可能有空调:

But there are other points where relation might occur as well. For example, both a car and a house might have air conditioning:

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类从OpenableProperty继承没有交流?在这种情况下更好的答案是提取接口和使用这些组成我们的类的功能,而不是继承:

Should TurnOnAirConditioning() be extracted to the base class too? What does it have to do with being an OpenableProperty? Could a JewelrySafe class inherit from OpenableProperty without an AC? The better answer in this situation is to extract Interfaces and use these to compose the functionality in our classes rather than inherit:

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

那么你的抽象可以食用这样:

Then your abstractions can be consumed as such:

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然后可以用于检查汽车,房子和JewelrySafe,而ThermostatService可以只用于测试车有房的交流。

The SecurityService could then be used to inspect the Car, House, and JewelrySafe, while the ThermostatService could be used only to test the AC of the Car and House.

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

这应该产生以下结果:

Which should produce the following results:

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!

这篇关于VB.NET接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆