使用接口继承或者干脆直接实现所有的接口? [英] Use interface inheritance or just implement all the interfaces directly?

查看:117
本文介绍了使用接口继承或者干脆直接实现所有的接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不一定看到接口继承接口,一个宏伟的好处。

I don't necessarily see a grandiose benefit of interfaces inheriting interfaces.

考虑以下三个接口,第三继承其它两个

Consider the following three interfaces, the third inheriting the other two

interface IOne {
}

interface ITwo {
}

// interface inheritance
interface IAll : IOne, ITwo {
}

时最好还是

class C : IOne, ITwo { ... }

class C : IAll { ... }

如果后者可以是有益的,为什么不只是创造IALL同时拥有IONE和ITwo的所有方法,而没有从他们那里继承?毕竟这是一个接口。

If the latter can be beneficial then why not just create IAll to have all the methods of both IOne and ITwo without inheriting from them? After all it's an interface.

时的接口继承实用,不只是有用吗?

Is interface inheritance practical, not just useful?

推荐答案

一个很好的思考界面中的继承的方法是完全忘记了它被称为继承。毕竟,明确了基地界面没有贡献的功能应用于来源接口;所有这有助于为承诺,会有一定的成员。

A good way to think about interface "inheritance" is to forget entirely that it is called "inheritance". After all, clearly the "base" interface contributes no functionality to the "derived" interface; all it contributes is the promise that there will be certain members.

一个类的派生关系通常意味着一个为一类 - 的关系。一个NullReferenceException是一类-的例外。接口派生关系并不意味着在所有 - 这让很少的意义说,的IEnumerable< T> 是一种 IDisposable的

A class derivation relationship usually implies an "is-a-kind-of" relationship. A NullReferenceException is-a-kind-of Exception. An interface derivation relationship doesn't imply that at all -- it makes very little sense to say that IEnumerable<T> is a kind of IDisposable.

相反,接口继承是指器具的IEnumerable&LT一类; T&GT; 是要还实施的IDisposable 要求。也就是说,接口继承并不意味着这是那种,而是每一个执行这也实现所需。

Rather, interface inheritance means "a type which implements IEnumerable<T> is required to also implement IDisposable. That is, interface inheritance does not mean "this is a kind of that", but rather "every implementation of this is required to also implement that".

这是否使之更引人注目?

Does that make it more compelling?

这篇关于使用接口继承或者干脆直接实现所有的接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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