我将如何知道何时创建界面?(How will I know when to create an interface?)

编程入门 行业动态 更新时间:2024-10-26 10:38:41
我将如何知道何时创建界面?(How will I know when to create an interface?)

我在开发学习中有一点我觉得我必须更多地了解接口。

我经常阅读他们,但似乎我无法把握。

我已经阅读过例子:动物基类,像“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.

更多推荐

本文发布于:2023-04-29 12:19:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1336270.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:我将   界面   interface   create

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!