在C#中实现接口 [英] Implementing Interfaces in C#

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

问题描述

我正在尝试学习C#编程.我以前对Visual Basic的知识有限.我真的很难理解OOP.我有几本书,但似乎所有本书都可以理解重要的面向对象编程知识,并且可以争夺它,或者至少对我而言.

我刚刚讲完了所有有意义的继承部分.我了解其背后的原因,现在我要处理接口,但是无论我如何努力,我都无法理解a)为什么需要它们或b)如何实现它们.

我真的很想学习,但是对于我的一生,我无法理解.如果有人愿意花一些时间对我轻松地进行解释,我将不胜感激.

----编辑----
好的,我想我知道他们现在使用的是什么.它们像模板一样使用,以确保实现它的任何类都包含接口中声明的所有方法和属性.当您需要从多个父级继承时(如我先前所认为的),不会使用它们.

因此,如果我有一个计算形状面积的程序,总结一下我在这些链接之一上正在阅读的内容:

我有一类用于计算圆形的面积,另一类用于计算三角形的面积.方法-说GetArea()是不同的,但是具有相同的函数并返回相同的值.然后,我可以创建一个接口IShapeArea来声明方法.然后,这可以确保Circle和Triangle类具有完全相同的名称和返回类型.另外,如果我想添加一个Square类,则再次必须遵循相同的命名约定.

这似乎是强制一致性的一种方式.

Hi, I am trying to learn programming in C#. I had previous, limited knowledge of Visual Basic. I am really struggling to get my head around OOP. I have several books but all seem to get to the important OOP stuff and race over it, or at least for me.

I have just got through the inheritance part which all made sense. I understand the reasoning behind it, I''m now up to Interfaces but no matter how hard I try, I can not understand a) why you need them or b) how to implement them.

I really desperately want to learn but for the life of me I can not get my head round this. I would really appreciate it if somebody would take the time to explain it in nice easy terms for me.

----Edit----
Ok, so I think I understand what they are used for now. They are used like a template to ensure that any class that implements it contains all of the methods and properties declared in the interface. They are not used when you need to inherit from more than one parent (as I previously thought).

So to summarize what I was reading on one of those links, if I had a program that calculated the area of a shape:

I have one class for calculating area for a circle and one for triangle. The methods - say GetArea() are different but have the same function and return the same value. I could then create an interface IShapeArea which declares the methods. This then ensures that the Circle and triangle Classes have the exact same names and return types. Also, if I wanted to add a Square class, I would again have to follow the same naming convention.

It seems to be a way of forcing consistency. Would that be an accurate assessment?

推荐答案

好吧,如果您对继承进行了排序,那么接口并不是很难.

我想您知道C#中的foreach吗?而且您可以给它任何形式的Collection(List< T> ;、数组等)?

之所以有效,是因为所有Collections都实现了一个名为IEnumerable的接口-实际上,您可以声明自己的类,实现IEnumerable接口,它将在foreach中愉快地工作.
发生这种情况的原因是,在实现接口时,您提供了一组定义接口的例程:IEnumerable的FindFirst和FindNext的等效项.因此,当foreach需要在列表中查找第一个元素时,它将调用IEnumerable FindFirst方法(您在类中实现了该方法),然后调用您的版本.每次再次遍历循环时,对于FindNext都是相同的.

接口的作用是允许一个类实现多种行为,并像该类是从接口派生的那样进行操作.由于C#仅允许您从单个类派生(但接口数量不受限制),因此它允许行为看起来像是多重继承.

接口和类之间的区别也非常简单:声明接口时,不允许声明任何代码-仅声明属性和方法的基本内容.实际的代码必须由实现该接口的类填充.

声明一个接口很简单:我不会演示它,因为MSDN做得更好. http://msdn.microsoft.com/en-us/library/87d83y5b (v = vs.80).aspx [
Ok, if you have inheritance sorted, then Interfaces aren''t too difficult.

I assume you know about foreach in C#? And that you can give it any form of Collection (List<T>, array, and so forth)?

This works because all Collections implement an Interface called IEnumerable - indeed, you can declare your own class, implement the IEnumerable interface and it will work happily in a foreach.
This happens because when you implement an interface, you provide a set of routines that define it: The equivalent of FindFirst and FindNext for IEnumerable. So when foreach needs to find the first element in the list, it calls the IEnumerable FindFirst method (which you implemented in your class) and your version gets called. The same for FindNext each time it goes round the loop again.

What interfaces do is allow a class to implement a number of behaviors and act as if the class was derived from the interface (sort of). Since C# will only let you derive from a single class, (but an unlimited number of interfaces) it allows behaviour that seems like multiple inheritance.

The difference between an Interface and a class is also pretty simple: when you declare an interface, you are not allowed to declare any code - just the bare-bones of the properties and methods. The actual code must be filled in by the class that implements the interface.

To declare an interface is simple: I won''t demo it, because MSDN does a much better job. http://msdn.microsoft.com/en-us/library/87d83y5b(v=vs.80).aspx[^]

They are a bit difficult to get your head round, but they are important, and you have probably already used them a lot more than you think! :laugh:


当您想说我想要具有这种行为的东西,但我不在乎它如何实现"时,可以使用一个接口. Griff提到的IEnumerable和foreach循环示例就是一个很好的例子-事物的内部表示形式是什么或如何实现都无关紧要,如果它实现IEnumerable,则可以由foreach循环使用.

如果您发现自己想通过某些操作传递对象的地方,就希望创建自己的接口,但是您不想指定这些操作的完成方式. (运行时多态性"仅意味着在运行时运行的实际代码因对象类型而异.)例如,在我的工作中,我们有一些图形代码,并且在其中创建了数据序列.我创建了一个IDataProvider,它提供了用于提供数据的挂钩,而不是该系列需要了解如何查找数据的工具,而该系列对此提出了要求.在其他地方,深入到应用程序的复杂部分,一些类实现了IDataProvider并找到了合适的数据(在不同的地方以不同的方式).

接下来,接口(和抽象基类,在某些情况下,如果有通用的最小功能在没有依赖的情况下有意义)将有助于模块化和减少依赖.因为您需要指定的只是您想要做的事情,所以接口几乎没有依赖项(仅依赖于声明中使用的类型;通常,您希望将它们限制为当前项目中的Framework类型和类型).可能,但有时也需要来自较低"层的类型.)
You use an interface when you want to say ''I want something with this behaviour, but I don''t care how it''s implemented''. The example of IEnumerable and foreach loops that Griff mentioned is an excellent one – it doesn''t matter what the internal representation of something is or how it does it, if it implements IEnumerable it can be used by a foreach loop.

You''d want to create your own interfaces if you find places where you know you want to be passed an object with some operations, but you don''t want to specify how those operations are done. (''Runtime polymorphism'' just means that the actual code that gets run at runtime is different depending on object type.) For example, in my work we have some graphing code, and as part of that we create data series. Instead of the series needing to know how to look up data, I created an IDataProvider which provides hooks for providing data, and the series asks that. Elsewhere, deep in complicated bits of the application, some classes implement IDataProvider and find the appropriate data (in different ways in different places).

That leads onto the next point, interfaces (and abstract base classes, in some cases, if there is a generic minimal functionality that makes sense without dependencies) help with modularisation and dependency reduction. Because all you need to specify is what you want something to be able to do, an interface has almost no dependencies (only on the types used in the declarations; generally you want to restrict those to Framework types and types in the current project, if possible, but sometimes types from ''lower'' layers are needed too).


我发现接口的概念 [此处 [ ^ ]是抽象类和接口之间的一些区别.
I found this[^] a fairly nice explanation.
Concept of Interface[^] could help you a little as well.

From a programming point of view, here[^] are some differences between abstract classes and interfaces.


这篇关于在C#中实现接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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