C# 当一个接口从另一个接口“继承”时,你怎么称呼它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/807217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What do you call it when one interface "inherits" from another?
提问by James Curran
If I have class B : A {}
如果我有 B 类:A {}
I say that "Class B inheritedclass A" or "class B derives from class A".
我说“B类继承了A 类”或“B 类派生自 A 类”。
However, if I instead have:
但是,如果我改为:
class B : ISomeInterface {}
it's wrong to say "B inherits ISomeInterface" -- the proper term is to say "B implementsISomeInterface".
说“B 继承了 ISomeInterface”是错误的——正确的说法是说“B实现了ISomeInterface”。
But, say I have
但是,说我有
interface ISomeInterface : ISomeOtherInterface {}
Now, it's still wrong to say "inherits", but it's now just as wrong to say "implements" since ISomeInterface doesn't implement anything.
现在,说“继承”仍然是错误的,但现在说“实现”同样是错误的,因为 ISomeInterface 没有实现任何东西。
So, what do you call that relationship?
那么,你怎么称呼这种关系呢?
采纳答案by Jon Skeet
I personally say "extends" and I thoughtthe C# spec uses that word as well somewhere (I can't find it now, unfortunately) - but I remember Eric Lippert saying he wasn't keen on it, and wanted to change it for 4.0.
我个人说“扩展”,我认为C# 规范在某处也使用了这个词(不幸的是,我现在找不到它) - 但我记得 Eric Lippert 说他并不热衷于它,并想改变它4.0.
I think it's good, because it shows that you're extending the contract specified by the original interface.
我认为这很好,因为它表明您正在扩展原始接口指定的合同。
EDIT: Having looked at the 3.0 spec...
编辑:看过 3.0 规范...
The spec sort of side-steps the issue in section 13.2. It talks about the members being inherited from the base interfaces. It talks about one classextending another, but not interfaces
规范在第 13.2 节中回避了这个问题。它讨论了从基本接口继承的成员。它讨论一个类扩展另一个类,但不讨论接口
EDIT: In the C# 5 spec, section 13.1.4, it uses inherits:
编辑:在 C# 5 规范的第 13.1.4 节中,它使用继承:
An interface can inherit from zero or more interface types
一个接口可以从零个或多个接口类型继承
So that's probably the best term to use.
所以这可能是最好的用语。
回答by Alex Martelli
I call it "extends".
我称之为“扩展”。
回答by Francis
you've got the definition wrong.
你的定义错了。
B : A means "B inherits from A".
B : A 表示“B 从 A 继承”。
when we say "B implements InterfaceA", that usually means InterfaceA does not have the definition to function - it's only prototypes (or, PURE in C++). However in C++ and most OOPL, the inherit and implement shares same syntax.
当我们说“B实现了InterfaceA”时,这通常意味着InterfaceA没有函数定义——它只是原型(或者,C++中的PURE)。但是在 C++ 和大多数 OOPL 中,继承和实现共享相同的语法。
So, InterfaceB : InterfaceA still means "InterfaceB inherits InterfaceA".
因此,InterfaceB : InterfaceA 仍然表示“InterfaceB 继承 InterfaceA”。
回答by TheFogger
Why would it be wrong to say InterfaceB "inherits" or "derives from" InterfaceA? "Implements" would be wrong, because InterfaceB doesn't provide an implementation. Semantically, deriving interfaces is very similar to deriving classes. C++, for example, doesn't distinguish between interfaces and classes at all.
为什么说 InterfaceB“继承”或“派生自”InterfaceA 是错误的?“实现”是错误的,因为 InterfaceB 不提供实现。从语义上讲,派生接口与派生类非常相似。例如,C++ 根本不区分接口和类。
回答by dsimcha
Usually, I call it overengineering.
通常,我称之为过度工程。
回答by user919426
As an authoritative sourceand in case you need a helpful quote, I came across info on MSDNthat describes "interface inheritance" with a practical example of the interface inheritance structure for the ComboBox, Control, Listbox and TextBox:
作为权威来源,如果您需要有用的引用,我在 MSDN 上看到了描述“接口继承”的信息,其中包含 ComboBox、Control、Listbox 和 TextBox 的接口继承结构的实际示例:
And with mention of an explicit base interface
并提到了一个显式的基本接口
An interface can inherit from zero or more interfaces, which are called the explicit base interfacesof the interface.
一个接口可以从零个或多个接口继承,这些接口称为接口的显式基接口。
Example:
例子:
interface IControl
{
void Paint();
}
interface ITextBox: IControl
{
void SetText(string text);
}
interface IListBox: IControl
{
void SetItems(string[] items);
}
interface IComboBox: ITextBox, IListBox {}
Source:
来源:
http://msdn.microsoft.com/en-us/library/aa664578%28VS.71%29.aspx
http://msdn.microsoft.com/en-us/library/aa664578%28VS.71%29.aspx