开发者

Interface Inheritance and Sub-Interfaces

interface IA : interface IB { ... }

So IB is the parent interface of IA, IA is the _____ of IB. What s开发者_开发技巧hould be put in the blank? sub-interface?


There's no inheritance relationship between .NET interfaces. This is evident if you use Reflection - typeof(IB).BaseType will be null, not IB.

Personally, I prefer to think of interfaces as contracts. In this case, "inheritance" of interfaces is really just strengthening the contract - so I'd say "contract IB implies IA". Eric Lippert seems to share this point of view:

He regards extension as being about reuse of implementation, whereas specifying a "base" interface is about saying that "any object fulfilling this contract must also fulfill this other interface".

Alternatively, "IA extends IB" sounds understandable while being technically correct (if we're talking about extending a contract), though perhaps somewhat misleading, as we're tightening the contract in "extended" interface, not relaxing it.

Yet another way is to go with what C# language specification uses. That one has a section on the topic titled "Base interfaces", and starts with the following sentence:

An interface can inherit from zero or more interface types, which are called the explicit base interfaces of the interface.

So there you have it. Though it should be noted that this is for C# 3.0; past versions of the spec actually used "extends" rather than "inherits" to describe the same thing, so there is still some possibility of confusion.


It is said that IA is the subinterface of IB, and IA extends IB.

So, IB is the superinterface of IA.

All classes based on these interfaces implements them.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜