asp.net – VB.NET接口
我不清楚为什么或何时使用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! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- ninject – 为从ASP.NET MVC和工作者应用程序使用RabbitMQ设
- asp.net – Azure服务结构:IReliableQueue
- Asp.NET Core+ABP框架+IdentityServer4+MySQL+Ext JS之显示
- 如何使用ASP.NET MVC实现站点而不使用Visual Studio?
- asp.net – Web服务中的循环引用
- Asp.net MVC Razor – 自定义javascript里面的if块
- asp.net – 如何防止低效的SQL查询减慢数据库服务器的速度
- asp.net – 在IIS上部署MVC应用程序时,我收到一个空白页面
- win10+CUDA8.0+VS2013环境配置
- asp.net-mvc – Asp.Net MVC中的管理区域
- Crystal Reports 13和Asp.Net 3.5
- asp.net – DNN外观可以使用空白元素而不是div来
- ASP.NET MVC:将自定义属性放入选择列表中的选项
- asp-classic – 我使用经典的ASP,IIS6和WinServe
- asp.net-mvc – 我应该如何测试从控制器发送电子
- 将ASP.net Web API用作服务层是否合适?
- ASP.NET MVC 4 – Ajax.BeginForm和html5
- 图表 – 在Asp.net mvc中实施Google Analytics
- asp.net-mvc – 可以在运行时更改数据注释吗? (
- ASP.NET MVC 3 jQuery验证;禁用不引人注意的OnKe