为什么我们要递归地实现接口?
我知道任何 Collection(这里我谈论的是常规非泛型)都应该实现 ICollection、IEnumerable 和 IList(如果是常规对象集合)或 IDictionary(如果是字典)。
[不过,我问的问题不是特定于集合的]
IList 派生自 ICollection,IEnumerable
ICollection 派生自 IEnumerable
难道仅仅让集合(例如 ArrayList)实现 IList 就足够了吗?
在对象浏览器中,显示集合类(例如ArrayList)正在实现IList、ICollection 和IEnumerator。
据我所知,即使我们指定所有三个集合,.Net 也只会接受一次定义。
但我的问题是,
是否有任何最佳实践或建议可以指导我们为集合类(或任何类似的类)指定所有三个接口?
或者只是对象浏览器的属性将其显示为 3 个单独的实现? 【刚刚查了一下,发现不是对象浏览器的属性。对象浏览器只显示类定义中指定的接口]
I understand that any Collection (here I am talking about regular non-generic) should have implemented ICollection, IEnumerable and IList incase of regular object collection or IDictionary in case of Dictionaries.
[Still, the question I ask is not specific to collections]
IList is derived from ICollection and IEnumerable
ICollection is derived from IEnumerable
Is it not just enough to make a collection (E.g. ArrayList) implement IList?
In the Object browser it is displaying that collection classes (E.g. ArrayList) are implementing IList, ICollection, and IEnumerator.
I understand that even if we specify all three Collections, .Net is going to accept the definitions only once.
But my question is,
Is there any best practice or recommendation that guides us to specify all three interfaces for the collection class (Or any class similar to this)?
Or is it just the propery of Object Browser that displays it as 3 separate implementations? [Just checked and found that its not the property of Object browser. Object browser just displays the interfaces that are specified in the class definition]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我相信只是对象浏览器以这种方式显示它。我刚刚尝试过:
将其加载到对象浏览器中会显示两个类的两个接口。
请注意,有时,如果您从另一个实现继承,可能需要重新声明接口 - 如果该接口之前已显式实现或以其他方式实现,它允许您重新实现接口一种非虚拟的方式。我不认为这里的情况是这样,但值得一提。
但一般来说,您绝对不必指定所有接口,而且我通常也不会这样做。
I believe it's just the object browser that displays it that way. I've just tried this:
Loading this into the object browser showed both interfaces on both classes.
Note that sometimes there can be a point to redeclaring an interface if you're inheriting from another implementation - it allows you to reimplement the interface if it's previously been implemented explicitly or in a non-virtual way. I don't believe that's the case here, but it's worth mentioning.
In general though, you definitely don't have to specify all the interfaces, and I wouldn't generally do so.
这就足够了。声明一个类:
这意味着您的 MyCollectionClass 实现了 IList、ICollection 和 IEnumerable。
这要么是对象浏览器的细节,要么基类通过指定所有接口简单地实现了集合类。然而,确实没有任何驱动理由这样做。
It is enough. Declaring a class:
That will mean your MyCollectionClass implements IList, ICollection, and IEnumerable.
This is either a detail of the Object Browser, or else the base class has simply implemented the collection classes by specifying all interfaces. There really isn't any driving reason to do so, however.
现在,我认为您在问
之间有什么区别?
:和
答案是,绝对没有。第二个版本使其设计对其他程序员来说更清晰一些,但任一版本都会生成相同的代码。
Now, I think you are asking, given
What's the difference between:
and
And the answer is, absolutely nothing. The second version makes it design a bit clearer to other programmers, but either will generate identical code.
我相信这只是为了让开发人员清楚,或者帮助加强接口结构。我的意思是,假设您有一个数据结构类的接口,并且实现了 IDataObject。然后,IDataObject 实现 ISecurable 和 ILoggable。您创建的常规类可以只实现 IDataObject,但如果 IDataObject 的创建者稍后更改实现并删除 ILoggable 该怎么办?这可能会改变代码的功能。因此,为了防止这种情况发生,当您创建从 IDataObject 继承的类时,您可以明确表示您还想实现 ISecurable 和 ILoggable,只是为了安全起见。
我不确定他们为什么对 IList 这样做,但这两个原因是我对原因的最佳猜测。
I believe this is just either to be clear to developers, or to help enforce the interface structure. What I mean is, imagine you have an interface for data structure classes, and you implement IDataObject. IDataObject then implements ISecurable, and ILoggable. Regular classes you create could just implement IDataObject, but what if the creator of IDataObject changes the implementation later and drops ILoggable? This may change the functionality of your code. So, to prevent this, when you create your class that inherits from IDataObject, you can explicitly say that you want to also implement ISecurable and ILoggable, just to be safe.
I don’t know for sure why they did that with IList, but those two reasons are my best guess on why.
为了通过接口访问对象,该对象的类定义必须显式定义它实现该接口...
例如,我可以有以下:
和以下类:
现在,狗确实在叫;但是,因为它没有声明它实现 IAnimal,所以我不能执行以下操作:
为了解决此问题,必须将 Dog 的定义更改为:
In order to access an object via an interface, the class definition for the object must explicitly define that it implements the interface...
For example, I can have the following:
and the following class:
Now, the dog does indeed yelp; however, because it doesn't declare that it implements IAnimal, I can't do the following:
In order to fix this the definition for Dog must be changed to: