一个接口是否应该继承另一个接口 [英] Should one interface inherit another interface

查看:173
本文介绍了一个接口是否应该继承另一个接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎找不到答案,只是想确保它是一个好的编码标准.我有许多不同类使用的接口A,并且不想更改接口A.我遇到了一个新的要求,该要求要求实现接口A的许多类都需要一个枚举,但是并不是所有的类都需要该枚举.我不希望不需要此新枚举的类来实现此新功能.因此,我创建了接口B,其中包含需要添加的新枚举.然后,我使接口B继承了接口A,这是我关心的问题,一个接口可以继承另一个接口是否可以?为了继续进行更改,我随后将需要新枚举的类更改为实现接口B而不是接口A,因为它是由接口B继承的.我考虑过在需要它们的类中实现这两个接口,但是我正在使用接口整个代码,并且只想使用一个接口浏览类,而不要使用两个接口.

我希望这已经足够清楚(可能很长),但是如果有人可以给我一些建议,或者我做对了或者我做错了,请告诉我.

谢谢!

解决方案

接口继承是一个很好的工具,尽管您仅应在接口B真正可替代接口A时使用它,而不仅仅是汇总松散相关的行为. /p>

很难确定它是否适合您的特定情况,但原则上使用该实践没有错.您始终可以在一流的API中看到它.仅从.NET框架中选择一个常见示例:

public interface ICollection<T> : IEnumerable<T>, IEnumerable

I can't seem to find an answer on this and just want to make sure it's an ok coding standard. I have Interface A that is used by many different classes and don't want interface A to change. I came across a new requirement that will require an enum to be needed by many of the classes that Implement Interface A, but not all the classes need this enum. I don't want the classes that don't require this new enum to Implement this new functionality. So I created Interface B that contains the new enum that I needed to add. I then made Interface B Inherit Interface A and this is my concern, Is it ok for one Interface to Inherit another Interface? To continue with my changes, I then changed the classes that needed the new enum to Implement Interface B instead of Interface A since it was Inherited by Interface B. I thought about Implementing both Interfaces in my classes that needed them but I'm using the Interface throughout the code and would like to just use one Interface for looking through classes and not two.

I hope this was clear enough (probably to long) but if anyone can give me some advice on this either I'm doing it right or I'm doing it wrong please let me know.

Thanks!

解决方案

Interface inheritance is an excellent tool, though you should only use it when interface B is truly substitutable for interface A, not just to aggregate loosely-related behaviors.

It's difficult to tell whether it is appropriate for your specific case, but there's nothing wrong using the practice in principle. You see it in the first-rate APIs all the time. To pick just one common example, from the .NET framework:

public interface ICollection<T> : IEnumerable<T>, IEnumerable

这篇关于一个接口是否应该继承另一个接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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