简体   繁体   English

如果其中一个边界是类,则必须首先在java泛型中指定它。 为什么?

[英]If one of the bounds is a class, it must be specified first in java generics. Why?

I was referring to this java docs . 我指的是这个java文档 If one of the bounds is a class, it must be specified first . 如果其中一个边界是类,则必须首先指定它 What I feel is it should allow in any order. 我觉得应该允许任何顺序。

Why do java has such restriction ? 为什么java有这样的限制? Is there any specific reason behind this? 这背后有什么具体原因吗?

Multiple Bounds 多个边界

The preceding example illustrates the use of a type parameter with a single bound, but a type parameter can have multiple bounds: 前面的示例说明了使用带有单个边界的类型参数,但是类型参数可以有多个边界:

A type variable with multiple bounds is a subtype of all the types listed in the bound. 具有多个边界的类型变量是绑定中列出的所有类型的子类型。 If one of the bounds is a class, it must be specified first. 如果其中一个边界是类,则必须首先指定它。 For example: 例如:

Class A { /* ... */ } 
interface B { /* ... */ }
interface C { /* ...
 */ }

class D <T extends A & B & C> { /* ... */ }

If bound A is not specified first, you get a compile-time error: 如果未首先指定绑定A,则会出现编译时错误:

 class D <T extends B & A & C> { /* ... */ }  // compile-time error ,but why ?

First off, there can only be a single class there; 首先,那里只能有一个班级; each Java class (except java.lang.Object , which is a special case as the root of the hierarchy) can only ever inherit from a single other class. 每个Java类( java.lang.Object除外,这是一个特殊情况作为层次结构的根)只能从一个其他类继承。 There can be multiple interfaces, but at most one class. 可以有多个接口,但最多只有一个类。 This enormously simplifies the processing of the type hierarchy and the object construction process. 这极大地简化了类型层次结构和对象构造过程的处理。

Given that, the language designers decided that the class has to be listed first in the bounds (if it is present at all). 鉴于此,语言设计者决定必须首先在类中列出类(如果它存在的话)。 There's actually no deep reason for that — the compiler could have coped just fine with enforcing the only-one-class restriction without the ordering — but it does make things somewhat easier to teach as there's a simple rule: if you're using a class as a generic type bound, put it first . 实际上没有深层次的理由 - 编译器可以在没有排序的情况下强制执行唯一的一类限制 - 但它确实使教学更容易,因为有一个简单的规则: 如果你正在使用一个类作为泛型类型绑定,把它放在第一位

Might be they just to group the interfaces together, separate from the class. 他们可能只是将接口分组在一起,与类分开。

If T was a class then it would look as following: 如果T是一个类,那么它看起来如下:

public class T extends A implements B, C {

But in generics there is no implements , and only extends . 但是在泛型中没有implements ,只能extends So, the constraint could be only to make us list the class itself first and then list the interfaces due to the lack of the key word implements . 因此,约束可能只是让我们首先列出类本身,然后由于缺少关键字implements而列出接口。

JLS # 4.4. Type Variables

Every type variable declared as a type parameter has a bound. 声明为类型参数的每个类型变量都有一个绑定。 If no bound is declared for a type variable, Object is assumed. 如果没有为类型变量声明绑定,则假定为Object。 If a bound is declared, it consists of either: 如果声明了绑定,则它包含:

  • a single type variable T , or 单个类型变量T ,或
  • a class or interface type T possibly followed by interface types I1 & ... & In 类或接口类型T可能后跟接口类型I1 & ... & In

I would say as "A type variable with multiple bounds is a subtype of all the types listed in the bound." 我会说"A type variable with multiple bounds is a subtype of all the types listed in the bound." and it seems Java asks you to indicate the extends First in a class declaration that would be the reason. 并且似乎Java要求您在类声明中指出extends First,这是原因。 (On the other hand a class can only extend one other class) (另一方面,一个类只能扩展另一个类)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM