我不清楚为什么或何时使用Interfaces.有人可以在控制台应用程序中使用VB.NET发布完整,简单和小型的接口示例.它是如何可扩展的?
解决方法
简而言之:赞成组合而不是继承
接口只是您希望一个或多个类支持的一组通用成员定义.关键是您必须在实现接口时明确提供功能.
您可以使用继承实现类似的结果,因为两个子类可以从基础继承全功能成员.但是继承的缺点是你的子类最终会对基类产生很大的依赖.
考虑以下类:
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和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!