为什么我需要接口? [英] Why I need Interface?

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

问题描述

可能重复:结果
接口:为什么我不能好像抓住它们结果
我为什么要使用接口?

我觉得这个问题是重复1000次,我再次询问遗憾。我真的找一个普通的简单的答案,为什么我需要明确的接口,或者请你解释一下我离不开接口实现的。

I think this question is repeated 1000 times and I'm sorry for asking again. I really looking for a plain simple answer why I explicitly need interface, or if you please explain something I can't achieve without Interface.

如果是多重继承,然后我。会要求你给我一个简单的例子,通过它我可以理解为什么我需要接口

If it is multiple inheritance then I would request you to give me one simple example through which I can understand why I need interface.

- THX提前

请注意:我问在.NET上下文这个问题(C#)语言

Note: I'm asking this question in context of .NET(C#) language

EDIT1 :现在每当我尝试学习的界面我的头脑告诉我 - >哥们你要勾勒出一个人的身体在一张白纸但你需要,你必须重新绘制轮廓,绘制所有身体部位填充颜色给他们再弄白床单真实的情况。那么,为什么我在浪费第一个白床单只是为了让轮廓。

Edit1: For now whenever I try to learn Interface my minds tells me --> "buddy you are going to outline someone's body in a white sheet" But you need another white sheet where you have to again draw the outline, draw all body parts fill colors to them to get the real picture". So why I'm wasting first white sheet just to get the outline.

推荐答案

理解的最简单方法接口是他们让不同的对象,以揭露常见功能。这使得程序员编写更simplier,更短的代码程序的接口,那么只要对象实现该接口它会工作。

The easiest way of understanding interfaces is that they allow different objects to expose COMMON functionality. This allows the programmer to write much simplier, shorter code that programs to an interface, then as long as the objects implement that interface it will work.

数据库提供商:

有许多不同的数据库供应商中,MySQL,MSSQL,Oracle等但是所有的数据库对象可以做同样的事情,所以你会发现数据库对象的多个接口。如果一个对象实现的IDbConnection那么它暴露方法的open()和close()。所以,如果我想我的计划是数据库提供商无关,我改写为接口,而不是特定的供应商。

There are many different database providers, MySQL, MSSQL, Oracle, etc. However all database objects can DO the same things so you will find many interfaces for database objects. If an object implements IDBConnection then it exposes the methods Open() and Close(). So if I want my program to be database provider agnostic, I program to the interface and not to the specific providers.

IDbConnection connection = GetDatabaseConnectionFromConfig()
connection.Open()
// do stuff
connection.Close()

通过编程看到的界面(的IDbConnection)我现在可以换出任何数据提供商我的配置,但我的代码保持完全相同的。这种灵活性是非常有用和易于维护。这样做的缺点是,我只能执行通用的数据库操作,可能没有充分利用每一个特定的供应商提供,以便在节目你有一个权衡一切的力量,你必须确定哪个方案将最有利于你。

See by programming to an interface (IDbconnection) I can now SWAP out any data provider in my config but my code stays the exact same. This flexibility can be extremely useful and easy to maintain. The downside to this is that I can only perform 'generic' database operations and may not fully utilize the strength that each particular provider offers so as with everything in programming you have a trade off and you must determine which scenario will benefit you the most.

类别:

如果您发现几乎所有集合实现这个接口被称为IEnumerable的。 IEnumerable的函数返回一个拥有的MoveNext(),当前一个IEnumerator,和reset()。这使得C#方便的通过您的收藏移动。它可以这样做的原因是因为它暴露了IEnumerable接口它知道对象公开它需要通过它的方法。这做了两件事。 1)foreach循环现在知道如何枚举的收集和2)现在可以申请强大的LINQ exprssions到您的收藏。同样,为什么界面是如此有用这里的原因是因为所有的藏品有共同的东西,他们可以通过移动。每个集合可以通过不同的方式(链表VS阵列)被移动,但是这是接口的美妙之处在于执行隐藏与无关的界面的消费者。的MoveNext()为您提供了集合中的下一个项目,它无关紧要如何做的。相当不错,是吧?

If you notice almost all collections implement this interface called IEnumerable. IEnumerable returns an IEnumerator which has MoveNext(), Current, and Reset(). This allows C# to easily move through your collection. The reason it can do this is since it exposes the IEnumerable interface it KNOWS that the object exposes the methods it needs to go through it. This does two things. 1) foreach loops will now know how to enumerate the collection and 2) you can now apply powerful LINQ exprssions to your collection. Again the reason why interfaces are so useful here is because all collections have something in COMMON, they can be moved through. Each collection may be moved through a different way (linked list vs array) but that is the beauty of interfaces is that the implementation is hidden and irrelevant to the consumer of the interface. MoveNext() gives you the next item in the collection, it doesn't matter HOW it does it. Pretty nice, huh?

多态性

当你正在设计自己的界面你只需要问自己一个问题。什么这些东西有什么共同点?一旦你找到所有的对象共享,抽象的那些属性/方法为一个接口,使每个对象可以从它继承的东西。然后,你可以进行编程反对使用一个接口多个对象。

When you are designing your own interfaces you just have to ask yourself one question. What do these things have in common? Once you find all the things that the objects share, you abstract those properties/methods into an interface so that each object can inherit from it. Then you can program against several objects using one interface.

当然,我不得不放弃我最喜欢的C ++多态例如,动物的例子。所有动物共有的某些特性。比方说,他们可以移动,说话,他们都有一个名称。因为我能发现什么我所有的动物都有共同点,我可以抽象这些素质到IAnimal接口。然后,我创建了一个熊对象,猫头鹰对象,都实现此接口的蛇对象。为什么你可以存储不同的对象在一起实现相同的接口的原因是因为接口代表了IS-A replationship。熊IS-A的动物,一只猫头鹰IS-A的动物,因为这所以它使我可以收集所有的动物。

And of course I have to give my favorite C++ polymorphic example, the animals example. All animals share certain characteristics. Lets say they can Move, Speak, and they all have a Name. Since I just identified what all my animals have in common and I can abstract those qualities into the IAnimal interface. Then I create a Bear object, an Owl object, and a Snake object all implementing this interface. The reason why you can store different objects together that implement the same interface is because interfaces represent an IS-A replationship. A bear IS-A animal, an owl IS-A animal, so it makes since that I can collect them all as Animals.

var animals = new IAnimal[] = {new Bear(), new Owl(), new Snake()} // here I can collect different objects in a single collection because they inherit from the same interface

foreach (IAnimal animal in animals) 
{
    Console.WriteLine(animal.Name)
    animal.Speak() // a bear growls, a owl hoots, and a snake hisses
    animal.Move() // bear runs, owl flys, snake slithers
}

您可以看到,即使这些动物用不同的方式完成每一个动作,我可以在一个统一的模型程序对所有这些,这只是接口的诸多好处之一。

You can see that even though these animals perform each action in a different way, I can program against them all in one unified model and this is just one of the many benefits of Interfaces.

于是再次与接口中最重要的事情是什么做的对象共有,这样就可以对以同样的方式不同对象编程。节省时间,创造更多的灵活的应用程序,隐藏复杂性/执行,机型现实世界的对象/情况下,许多其他好处。

So again the most important thing with interfaces is what do objects have in common so that you can program against DIFFERENT objects in the SAME way. Saves time, creates more flexible applications, hides complexity/implementation, models real-world objects / situations, among many other benefits.

希望这有助于。

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

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