为什么 Java 泛型不能实现接口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18819068/
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
Why can't a Java Generic implement an Interface?
提问by Q Zhao-Liu
Is there a logical reason why E cannot implement my interface HasName?
E 无法实现我的接口 HasName 是否有合乎逻辑的原因?
public class SinglyLinkedList<E extends HasName> {
// stuff...
}
采纳答案by Bohemian
The extends
keyword applies to interfaces too. That is:
该extends
关键字也适用于接口。那是:
public class SinglyLinkedList<E extends HasName> {
Means that E
must be a type that extends a class, or implements an interface, called HasName
.
意味着E
必须是扩展类或实现接口的类型,称为HasName
.
It is not possible to code E implements HasName
- that is implied by E extends HasName
.
无法编码E implements HasName
- 这由E extends HasName
.