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! (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net-mvc – DataAnnotations StringLength属性MVC – 没
- asp.net-web-api – 如何在ASP.NET 5和MVC 6中启用跨源请求
- asp.net-mvc – 使用WebForm的MVC HtmlHelper
- asp.net mvc调试器抛出SEHException
- 配置 – 升级到ASP.NET 4.5后出现404错误
- asp.net-mvc-3 – 在MVC 3视图模型上使用Editable属性
- asp.net-mvc – 如何使用绑定前缀?
- 谈基于.net平台开发中的模式窗体
- 在asp.net.mvc中,什么是正确的方法来引用css内的图像
- asp.net-mvc – Visual Studio 2013持有的App_global.asax.
- asp.net-mvc – 在构建时尝试构建ASP MVC视图时出
- asp.net-mvc-3 – 在使用Unity容器时为此对象异常
- 如何在ASP.NET Core MVC 6中强制执行小写路由?
- Asp.Net Cache缓存使用代码
- asp.net – HttpHandler 101失败
- 部署ASP.Net MVC应用程序的最便宜的方法是什么?
- asp.net – 你对Windows Workflow Foundation有什
- asp.net – “无法启动IIS Express Web服务器”错
- VS 2015预览缺少“ASP.NET 5 Web应用程序”项目类
- 从ASP.NET服务器控件动态添加CSS文件
