问题描述
我正在开发学习中,我觉得我必须了解更多有关接口的信息.
我经常读过它们,但似乎我无法掌握它们.
我已经阅读了以下示例:动物基课,带有诸如"步行"," run"," getlegs"等事物的Ianimal界面 - 但是我从未在做某件事,觉得"嘿,我应该在这里使用接口!"
我想念什么?为什么我要掌握这是一个很难的概念!我只是对我可能永远不会意识到对一个人的具体需求的事实感到恐惧 - 主要是由于理解它们的某些方面!就开发人员而言,这让我觉得我错过了最重要的东西!如果有人有这样的经验并取得了突破,我将感谢一些有关如何理解这一概念的提示.谢谢.
推荐答案
它解决了这个具体问题:
您的A,B,C,D为4种不同类型.在您的代码上,您都有类似的东西:
a.Process(); b.Process(); c.Process(); d.Process();
为什么不实现它们,然后做
List<IProcessable> list; foreach(IProcessable p in list) p.Process();
当您添加(例如50种类型的类型的类型类型的类型)时,这将更好地扩展.
另一个具体问题:
您是否曾经看过system.linq.它定义了大量的扩展方法,这些方法在实现IEnumerable的任何类型上运行.因为任何实现IEnumerable的事物基本上都说"我以无效的foreach类型模式支持迭代",因此您可以针对任何枚举类型定义复杂的行为(count,count,count,count,where,select等).
其他推荐答案
我非常喜欢吉米的答案,但是我觉得我需要添加一些东西.整个过程的关键是Iprocess Able 中的"能够".它表示实现界面的对象的功能(或属性,但意为"内在质量",而不是C#属性的意义). ianimal可能不是接口的好示例,但是如果您的系统有很多可以走路的东西,则可以拥有一个很好的界面.您可能会从狗,牛,鱼,蛇等动物派生.前两个可能会实现iwalkable,后两个人不会走路,所以他们不会.现在,您问:"为什么不只是另一个超级阶级,步行,那只狗和牛来自呢?".答案是,当您在继承树外面完全有一些可以行走的东西,例如机器人.机器人可以实施iwalkable,但可能不会源自动物.如果您想要可以走路的东西列表,则可以将其键入iWalkable,并且可以将所有步行动物加上机器人放在列表中.
现在,用ipersististsions的软件替换iwalkable,类比变得更接近您在真实程序中看到的东西.
其他推荐答案
当实现相同功能时,请使用接口.
当您需要共享常见的具体实现时,请使用摘要/基类.
问题描述
I'm at a point in my development learning where I feel like I must learn more about interfaces.
I frequently read about them but it just seems like I cannot grasp them.
I've read examples like: Animal base class, with IAnimal interface for things like 'Walk', 'Run', 'GetLegs', etc - but I've never been working on something and felt like "Hey I should use an interface here!"
What am I missing? Why is it such a hard concept for me to grasp! I am just intimidated by the fact that I might not ever realize a concrete need for one - mostly due to some missing aspect of understanding them! It makes me feel like I'm missing something up top in terms of being a developer! If anyone has had an experience like this and had a breakthrough I would appreciate some tips on how to understand this concept. Thank you.
推荐答案
it solves this concrete problem:
you have a, b, c, d of 4 different types. all over your code you have something like:
a.Process(); b.Process(); c.Process(); d.Process();
why not have them implement IProcessable, and then do
List<IProcessable> list; foreach(IProcessable p in list) p.Process();
this will scale much better when you add, say, 50 types of classes that all do the same thing.
Another concrete problem:
Have you ever taken a look at System.Linq.Enumerable? It defines a ton of extension methods that operate on any type that implements IEnumerable. Because anything that implements IEnumerable basically says "I support iteration in a unordered foreach-type pattern", you can define complex behaviors (Count, Max, Where, Select, etc.) for any enumerable type.
其他推荐答案
I like Jimmy's answer a lot, but I feel I need to add something to it. The key to the whole thing is the "able" in IProcessable . It indicates a capability (or property, but meaning "intrinsic quality", not in the sense of C# properties) of the object that implements the interface. IAnimal is probably not a good example for an interface, but IWalkable might be a good interface to have if your system has many things that can walk. You might have classes derived from Animal such as Dog, Cow, Fish, Snake. The first two would probably implement IWalkable, the latter two don't walk, so they wouldn't. Now you ask "why not just have another superclass, WalkingAnimal, that Dog and Cow derive from?". The answer is when you have something completely outside the inheritance tree that also can walk, such as a robot. Robot would implement IWalkable, but probably wouldn't derive from Animal. If you want a list of things that can walk, you type it as IWalkable and you can put all walking animals plus robots in the list.
Now replace IWalkable with something more software-y like IPersistable, and the analogy becomes much closer to what you'd see in a real program.
其他推荐答案
Use interfaces when implementations of the same functionality will differ.
Use a abstract/base classes when you need to share a common concrete implementation.