.NET接口的说明 [英] .net Interface explanation

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

问题描述

据我所知,在.NET中的接口定义了接口和一个类继承它之间的合同。刚刚得到的工作做上大量使用的数据访问层接口的一个项目,它让我思考。 。 。什么大不了的?当我有一个新的方法添加到DAL,我只好随它添加到继承接口的类创建的接口中的方法签名,当然还有方法的DAL,从而创造额外工作。请告诉我关于接口以及为什么大不了的,我会想创建额外的工作为自己?

I understand that an interface in .Net defines a contract between the interface and a class that inherits it. Having just gotten done working on a project that made heavy use of an interface for the Data Access Layer, it got me thinking . . . whats the big deal? When I had to add a new method to the DAL, I had to create the method signature in the interface along with adding it to the class that inherited the interface, and of course the method to the DAL, thus creating "extra work". Whats the big deal about interfaces and why would I want to create extra work for myself?

推荐答案

什么是关于接口大不了的?

一旦定义了合同,你可以换出实现,而无需担心破坏你的code中的其余部分。

Once you define the contract, you can swap out implementations without worrying about breaking the rest of your code.

考虑,你有表演很差code,它是利用一个名单,其中的情况; T> 在.NET中。如果使用硬执行的名单,其中,T> ,还有你要改变的实施,打破更多的code的好机会。

Consider the situation where you have poor performing code that is making use of a List<T> in .NET. If you use the hard implementation of List<T>, there's a good chance you're going to break more code by changing the implementation.

如果你使用的IList&LT; T&GT; 的IEnumerable&LT; T&GT; 你就可以交换名单,其中,T&GT; 的LinkedList&LT; T&GT; (或任何实现你所选择的接口),并在一个地方解决问题,而不是需要接触所有的code。

If you were using IList<T> or IEnumerable<T> you would be able to swap List<T> for LinkedList<T> (or anything implementing your chosen interface) and fix the issue in one spot rather than having to touch all of your code.

在年底...它是关于对 <一个href="http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming#C.23">Polymorphism.

In the end...it's about about Polymorphism.

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

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