接口有什么作用? [英] What does an interface do?

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

问题描述

大家好,

我正在使用一本不解释接口的书.
我想知道界面真正的作用,并且我也在使用MEF.
请把我链接到一个好的网站,或者给我一个好的答案.
它还告诉我创建一个接口,但没有告诉我在该接口中创建任何代码.是否有应默认输入的内容或其他内容.
请让我知道.

谢谢.

Hi guys,

I am using a book which does not explain about interfaces .
I want to know what a interface really does and I am also using MEF.
Please link me to a good site or give me a good answer.
It also tells me to make an interface but doesn''t tell me to create any code in that interface.Is there something that i should type by default or something .
Please let me know.

Thank you.

推荐答案

您是否读过MSDN所说的内容?这是很有解释性的.

http://msdn.microsoft.com/en-us/library/ms173156 (v = vs.110).aspx [
Did you read what MSDN says about it? It is quite explanatory.

http://msdn.microsoft.com/en-us/library/ms173156(v=vs.110).aspx[^]


接口本身不执行任何操作.您可以将接口视为合同,该合同描述了类型要想履行该合同时必须声明的签名方法.然后,您可以使用该类型的实例,以确保它可以完全履行合同,并且使用该实例的代码甚至不必知道该具体类型(请参阅:针对接口进行编程,而不是具体的实现). >
一个空的接口甚至可以用作某种标记,尽管我认为在大多数情况下,属性会更好.没有规定任何方法或属性的接口可以从其继承中添加到任何类型定义中.在您的代码中,您可以检查一种类型是否实现了所说的(空)接口,如果该类型可以实现该情况,则可以做一些事情.
最好的问候,

曼弗雷德(Manfred)
An Interface does not do anything on it''s own. You can consider an Interface to be a contract which describes what method(s) with which signature(s) a type has to declare if it wants to fullfill that contract. You can then use an instance of that type in the sure knowledge that it fullfills the contract and the code using that instance does not even have to be aware of that concrete type (See: Programming against interfaces instead of concrete implementations).

An empty Interface can even be used as some kind of marker, even though I think Attributes are better for that most of the time. An interface that does not prescribe any method or property can be added to any type definition as inheriting from it. In your code you can check if a type implements said (empty) interface and if the type does you can do something fitting that situation.

Best Regards,

Manfred




通过以下论坛及其回应.它说明Why we need an Interface
http://www.dotnetspider.com/forum/13184-OOPS- Why-we-need-an-Interface.aspx [ ^ ]
Hi,

Go through following forum and its responses. It explains Why we need an Interface
http://www.dotnetspider.com/forum/13184-OOPS-Why-we-need-an-Interface.aspx[^]


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

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