我在开发学习中有一点我觉得我必须更多地了解接口。
我经常阅读他们,但似乎我无法把握。
我已经阅读过例子:动物基类,像“Walk”,“Run”,“GetLegs”等等的IAnimal接口 - 但是我从来没有在做过某些事情,感觉像“我应该使用一个接口这里!”
我失踪了什么 为什么这么难的概念让我掌握! 我只是因为我可能没有意识到一个具体的需要 - 主要是由于一些缺乏认识的方面的事实而被吓倒! 这让我觉得我在开发商方面缺少一些顶点的东西! 如果任何人有这样的经历,并有一个突破,我会欣赏一些关于如何理解这个概念的技巧。 谢谢。
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.
最满意答案
它解决了这个具体问题:
你有一个,b,c,d的4种不同的类型。 你的代码全部都有:
a.Process(); b.Process(); c.Process(); d.Process();为什么不让他们实现IProcessable,然后呢
List<IProcessable> list; foreach(IProcessable p in list) p.Process();当你添加50个类,所有这些都做同样的事情时,这会更好一些。
另一具体问题:
你有没有看过System.Linq.Enumerable? 它定义了大量的实现IEnumerable的任何类型的扩展方法。 因为实现IEnumerable的任何东西基本上都说“我以无序的foreach-type模式支持迭代”,所以可以为任何可枚举类型定义复杂的行为(Count,Max,Where,Select等)。
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.
更多推荐
发布评论