I wonder if there is a special reason in Java for using always "extends
" rather than "implements
" for defining bounds of typeparameters.
我想知道在Java中是否有一个特殊的原因,因为使用总是“扩展”而不是“实现”来定义类型参数的界限。
Example:
例子:
public interface C {}
public class A<B implements C>{}
is prohibited but
是被禁止的,但
public class A<B extends C>{}
is correct. What is the reason for that?
是正确的。原因是什么?
7 个解决方案
#1
292
There is no semantic difference in the generic constraint language between whether a class 'implements' or 'extends'. The constraint possibilities are 'extends' and 'super' - that is, is this class to operate with assignable to that other one (extends), or is this class assignable from that one (super).
在类“实现”或“扩展”之间的通用约束语言中没有语义差异。约束的可能性是“扩展”和“超级”——也就是说,这个类是与其他一个(扩展)的赋值操作的,或者是这个类可以从那个(super)中指定的。
#2
27
The answer is in here :
答案就在这里:
To declare a bounded type parameter, list the type parameter's name, followed by the
extends
keyword, followed by its upper bound […]. Note that, in this context, extends is used in a general sense to mean eitherextends
(as in classes) orimplements
(as in interfaces).要声明一个有界类型参数,请列出类型参数的名称,然后是扩展关键字,然后是它的上界[…]。注意,在这个上下文中,扩展被用于一般意义上,表示扩展(如在类中)或实现(如在接口中)。
So there you have it, it's a bit confusing, and Oracle knows it.
这就有点让人困惑了,Oracle知道。
#3
14
Probably because for both sides (B and C) only the type is relevant, not the implementation. In your example
可能因为对于双方(B和C),只有类型是相关的,而不是实现。在你的例子
public class A<B extends C>{}
B can be an interface as well. "extends" is used to define sub-interfaces as well as sub-classes.
B也可以是一个接口。“扩展”用于定义子接口和子类。
interface IntfSub extends IntfSuper {}
class ClzSub extends ClzSuper {}
I usually think of 'Sub extends Super' as 'Sub is like Super, but with additional capabilities', and 'Clz implements Intf' as 'Clz is a realization of Intf'. In your example, this would match: B is like C, but with additional capabilities. The capabilities are relevant here, not the realization.
我通常认为“Sub - extends Super”是“Sub - is like Super,但有附加功能”,“Clz实现Intf”作为“Clz是Intf的实现”。在您的示例中,这将匹配:B类似于C,但具有额外的功能。这里的功能是相关的,而不是实现。
#4
7
Here is a more involved example of where extends is allowed and possibly what you want:
这里有一个更复杂的例子,其中扩展是允许的,也可能是你想要的:
public class A<T1 extends Comparable<T1>>
公共类A
#5
6
It may be that the base type is a generic parameter, so the actual type may be an interface of a class. Consider:
可能是基类型是一个泛型参数,所以实际类型可能是类的接口。考虑:
class MyGen<T, U extends T> {
Also from client code perspective interfaces are almost indistinguishable from classes, whereas for subtype it is important.
同样,从客户端代码的角度来看,接口与类几乎是不可区分的,而对于子类型来说,它是重要的。
#6
2
It's sort of arbitrary which of the terms to use. It could have been either way. Perhaps the language designers thought of "extends" as the most fundamental term, and "implements" as the special case for interfaces.
这是一种任意使用的术语。它本来可以是任何一种方式。也许语言设计者认为“扩展”是最基本的术语,而“实现”是接口的特殊情况。
But I think implements
would make slightly more sense. I think that communicates more that the parameter types don't have to be in an inheritance relationship, they can be in any kind of subtype relationship.
但我认为实施会更有意义。我认为,更多的沟通,参数类型不一定要在继承关系中,它们可以在任何类型的子类型关系中。
The Java Glossary expresses a similar view.
Java术语表表达了类似的观点。
#7
-2
Because interfaces are just classes, except they don't have attributes or implementations. The only use of the keyword "implements" is to allow a class to inherit multiple interfaces, but not multiple classes, and we can see it in the code. I don't know if they will specify this in the future, but this is not a must.
因为接口只是类,但它们没有属性或实现。关键字“实现”的唯一用途是允许类继承多个接口,而不是多个类,我们可以在代码中看到它。我不知道他们是否会在将来指定这个,但这不是必须的。
#1
292
There is no semantic difference in the generic constraint language between whether a class 'implements' or 'extends'. The constraint possibilities are 'extends' and 'super' - that is, is this class to operate with assignable to that other one (extends), or is this class assignable from that one (super).
在类“实现”或“扩展”之间的通用约束语言中没有语义差异。约束的可能性是“扩展”和“超级”——也就是说,这个类是与其他一个(扩展)的赋值操作的,或者是这个类可以从那个(super)中指定的。
#2
27
The answer is in here :
答案就在这里:
To declare a bounded type parameter, list the type parameter's name, followed by the
extends
keyword, followed by its upper bound […]. Note that, in this context, extends is used in a general sense to mean eitherextends
(as in classes) orimplements
(as in interfaces).要声明一个有界类型参数,请列出类型参数的名称,然后是扩展关键字,然后是它的上界[…]。注意,在这个上下文中,扩展被用于一般意义上,表示扩展(如在类中)或实现(如在接口中)。
So there you have it, it's a bit confusing, and Oracle knows it.
这就有点让人困惑了,Oracle知道。
#3
14
Probably because for both sides (B and C) only the type is relevant, not the implementation. In your example
可能因为对于双方(B和C),只有类型是相关的,而不是实现。在你的例子
public class A<B extends C>{}
B can be an interface as well. "extends" is used to define sub-interfaces as well as sub-classes.
B也可以是一个接口。“扩展”用于定义子接口和子类。
interface IntfSub extends IntfSuper {}
class ClzSub extends ClzSuper {}
I usually think of 'Sub extends Super' as 'Sub is like Super, but with additional capabilities', and 'Clz implements Intf' as 'Clz is a realization of Intf'. In your example, this would match: B is like C, but with additional capabilities. The capabilities are relevant here, not the realization.
我通常认为“Sub - extends Super”是“Sub - is like Super,但有附加功能”,“Clz实现Intf”作为“Clz是Intf的实现”。在您的示例中,这将匹配:B类似于C,但具有额外的功能。这里的功能是相关的,而不是实现。
#4
7
Here is a more involved example of where extends is allowed and possibly what you want:
这里有一个更复杂的例子,其中扩展是允许的,也可能是你想要的:
public class A<T1 extends Comparable<T1>>
公共类A
#5
6
It may be that the base type is a generic parameter, so the actual type may be an interface of a class. Consider:
可能是基类型是一个泛型参数,所以实际类型可能是类的接口。考虑:
class MyGen<T, U extends T> {
Also from client code perspective interfaces are almost indistinguishable from classes, whereas for subtype it is important.
同样,从客户端代码的角度来看,接口与类几乎是不可区分的,而对于子类型来说,它是重要的。
#6
2
It's sort of arbitrary which of the terms to use. It could have been either way. Perhaps the language designers thought of "extends" as the most fundamental term, and "implements" as the special case for interfaces.
这是一种任意使用的术语。它本来可以是任何一种方式。也许语言设计者认为“扩展”是最基本的术语,而“实现”是接口的特殊情况。
But I think implements
would make slightly more sense. I think that communicates more that the parameter types don't have to be in an inheritance relationship, they can be in any kind of subtype relationship.
但我认为实施会更有意义。我认为,更多的沟通,参数类型不一定要在继承关系中,它们可以在任何类型的子类型关系中。
The Java Glossary expresses a similar view.
Java术语表表达了类似的观点。
#7
-2
Because interfaces are just classes, except they don't have attributes or implementations. The only use of the keyword "implements" is to allow a class to inherit multiple interfaces, but not multiple classes, and we can see it in the code. I don't know if they will specify this in the future, but this is not a must.
因为接口只是类,但它们没有属性或实现。关键字“实现”的唯一用途是允许类继承多个接口,而不是多个类,我们可以在代码中看到它。我不知道他们是否会在将来指定这个,但这不是必须的。