考虑这个程序:
public class xx<T> {
<T> Iterable<T> createIterable(Class<T> cls) {
return null;
}
Iterable<? extends Number> createNumberIterable(boolean floatingPoint) {
return this.createIterable(floatingPoint ? Integer.class : Float.class);
}
}
Run Code Online (Sandbox Code Playgroud)
在Java 7下,它编译:
$ java -version
java version "1.7.0_45"
Java(TM) SE Runtime Environment (build 1.7.0_45-b18)
Java HotSpot(TM) 64-Bit Server VM (build 24.45-b08, mixed mode)
$ javac xx.java
$
Run Code Online (Sandbox Code Playgroud)
在Java 8下它没有:
$ java -version
java version "1.8.0_40"
Java(TM) SE Runtime Environment (build 1.8.0_40-b25)
Java HotSpot(TM) 64-Bit Server VM (build 25.40-b25, mixed mode)
$ javac xx.java
xx.java:8: error: method createIterable in class xx<T#2> cannot be applied to given types;
return this.createIterable(floatingPoint ? Integer.class : Float.class);
^
required: Class<T#1>
found: floatingPo[...]class
reason: inferred type does not conform to equality constraint(s)
inferred: Float
equality constraints(s): Float,Integer
where T#1,T#2 are type-variables:
T#1 extends Object declared in method <T#1>createIterable(Class<T#1>)
T#2 extends Object declared in class xx
1 error
$
Run Code Online (Sandbox Code Playgroud)
这是真的:
如果答案是#1,您能用正常语言解释JLS不允许这样做的原因,使用明显的解释吗?
(注意:请不要解释如何解决问题,这不是问题)
旧行为既不是错误,新行为也不是错误。条件表达式类型的规则变得更加复杂,这在许多情况下都有帮助,并且对您来说并没有真正的伤害。
编译器不允许这样做,因为Integer.class和 的类型Float.class无法比较。没有任何类型T可以成为和Class<T>的超类型。Class<Integer>Class<Float>
| 归档时间: |
|
| 查看次数: |
189 次 |
| 最近记录: |