java java中的超级接口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1065160/
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
superinterface in java
提问by Michael Myers
What is super-interface is java? what is the purpose of super-interface?
什么是java超接口?超级接口的目的是什么?
回答by Michael Myers
Here's an example:
下面是一个例子:
public interface A {
void doSomething();
}
public interface B extends A {
void doSomethingElse();
}
In this example, Ais a superinterfaceof B. It's like being a superclass. Any class implementing Bnow also implements Aautomatically, and must provide an implementation of both doSomething()and doSomethingElse().
在这个例子中,A是一个超接口的B。这就像是一个超类。任何实现类B现在还实现了A自动的,必须同时提供的实现doSomething()和doSomethingElse()。
回答by Hank Gay
If you have
如果你有
public interface Bar extends Foo
then Foowould be the super-interface of Bar. This declares that all instances of Barare also instances of Fooand can be treated as such, so you could pass a Barinstance wherever you needed to pass a Foo, etc.
那么Foo将是Bar. 这声明了 的所有实例Bar也是 的实例Foo并且可以这样对待,因此您可以Bar在需要传递 aFoo等的任何地方传递一个实例。
回答by Tom
Basically, when an interface extends from other interface, it is forcing the class that implements it to implement methods in both interfaces.
基本上,当一个接口从其他接口扩展时,它会强制实现它的类在两个接口中实现方法。
If an extends clause is provided, then the interface being declared extends each of the other named interfaces and therefore inherits the member types, methods, and constants of each of the other named interfaces. These other named interfaces are the direct superinterfaces of the interface being declared. Any class that implements the declared interface is also considered to implement all the interfaces that this interface extends.
如果提供了 extends 子句,则被声明的接口会扩展每个其他命名接口,因此继承每个其他命名接口的成员类型、方法和常量。这些其他命名接口是所声明接口的直接超接口。任何实现声明接口的类也被认为实现了该接口扩展的所有接口。
You can have method overriding also :)
您也可以覆盖方法:)
http://java.sun.com/docs/books/jls/second_edition/html/interfaces.doc.html
http://java.sun.com/docs/books/jls/second_edition/html/interfaces.doc.html
回答by Uri
Interfaces in Java can extend one another, so that the extending interface supports all the things provided by the parent and the things it provides itself. For example, a Set has unique operations, but also supports everything that a Collection does.
Java中的接口可以相互扩展,这样扩展的接口就支持父提供的所有东西和它自己提供的东西。例如,Set 具有独特的操作,但也支持 Collection 所做的一切。
The interfaces from which you are extending are considered super-interfaces. Note that an interface can extend multiple interfaces and therefore has multiple super-interfaces.
您从中扩展的接口被视为超级接口。请注意,一个接口可以扩展多个接口,因此具有多个超级接口。
回答by Mohammadreza Khatami
when you have 2 interfaces with some related (same) field or methods. you should use one as superInterface which the second will extend, you shouldn't duplicate it. this is because simplicity and clarity to see what is going on exactly.
当您有 2 个具有某些相关(相同)字段或方法的接口时。您应该使用一个作为第二个将扩展的 superInterface,您不应该复制它。这是因为简单和清晰地看到究竟发生了什么。
回答by pob
The term superinterface has slightly different uses depending on the context of (a) interfaces only, and (b) classes, as described below:
术语超级接口的用法略有不同,具体取决于 (a) 仅接口和 (b) 类的上下文,如下所述:
(a) given interfaces A, and B, when B extends A then A is a superinterface of B (the explanation given above by Tom.)
(a) 给定接口 A 和 B,当 B 扩展 A 时,A 是 B 的超接口(Tom 上面给出的解释。)
see jls8, 8.1.5
(b) given class X, and interface C, when X implements C, then C is a direct superinterface of X.
(b) 给定类 X 和接口 C,当 X 实现 C 时,则 C 是 X 的直接超接口。
see jls8, 9.1.3
The second case here is not clear from any of the prior explanations. That is, in case (b) there isn't any requirement for a hierarchical chain of interfaces in order to use the term superinterface.
从先前的任何解释中都不清楚这里的第二种情况。也就是说,在情况 (b) 中,为了使用术语“超级接口”,不需要任何层次结构的接口链。

