如何为通用接口类型变量再现java编译/运行时错误,这些变量可能不是同时有两个param接口的子类型? [英] How to reproduce java compile/runtime error for generic interface type variables that may not be a subtype of two param interfaces at same time?
问题描述
我正在阅读 Java SE 6规范
a>,然后发现一些我无法复制的令人困惑的东西:
类型变量不能同时 是两个接口类型的子类型,它们是相同通用接口的不同参数化。
我写了下面的代码:
接口奇数{}
接口偶{}
接口奇怪的扩展奇数,偶{}
接口InterfaceOne< O扩展奇数,E扩展偶数> {}
接口InterfaceTwo< O扩展奇数,E扩展偶数>扩展奇数,偶{}
public class Test {
public static void main(String [] args){
//期望编译错误
InterfaceOne< Strange,奇怪> t1 = new InterfaceOne<奇怪,奇怪>(){};
//期望编译错误
InterfaceTwo< Strange,InterfaceTwo> t2 = new InterfaceTwo< Strange,InterfaceTwo>(){};
System.out.println(+ t1 + t2);
$ b $ p
$ b 上面的代码应该不会编译,但是它确实如此。
如何重现由规格预测的错误?
解决方案据我所知,泛型类型不能绑定到两个或多个接口,其中这些接口从使用泛型参数的另一个接口扩展而来,绑定的接口对泛型使用不同的类型。我已经写了一个这样的例子:
interface简单< T> {T aMethod(); }
接口SimpleString扩展简单< String> {}
接口SimpleInteger扩展简单<整数> {}
public class CompilerError {
public< T extends SimpleString& SimpleInteger> void(here interesting){
System.out.println(interesting.aMethod());
$ / code>
使用javac 1.6.0_32-ea,以下编译器错误:
CompilerError.java:5:简单不能用不同的参数继承:< java.lang.String>和< java.lang.Integer>
public< T扩展SimpleString& SimpleInteger>在这里无效(T有趣){
^
1错误
I was reading the Java SE 6 specs and then found some confusing stuff that a I can't reproduce:
A type variable may not at the same time be a subtype of two interface types which are different parameterizations of the same generic interface.
I wrote the following code:
interface Odd {}
interface Even {}
interface Strange extends Odd, Even {}
interface InterfaceOne<O extends Odd, E extends Even> {}
interface InterfaceTwo<O extends Odd, E extends Even> extends Odd, Even {}
public class Test {
public static void main(String[] args) {
//Expecting compilation error
InterfaceOne<Strange, Strange> t1 = new InterfaceOne<Strange, Strange>(){};
//Expecting compilation error
InterfaceTwo<Strange, InterfaceTwo> t2 = new InterfaceTwo<Strange, InterfaceTwo>(){};
System.out.println("" + t1 + t2);
}
}
The code above is supposed to do not compile, but it does.
How to reproduce the error predicted by specification?
解决方案 From what I understand, the generic type cannot be bound to two or more interfaces where these interfaces extend from another interface that employs a generic argument and the bound interfaces use different type for the generic. I've wrote an example of this:
interface Simple<T> { T aMethod(); }
interface SimpleString extends Simple<String> {}
interface SimpleInteger extends Simple<Integer> {}
public class CompilerError {
public <T extends SimpleString & SimpleInteger> void here(T interesting) {
System.out.println(interesting.aMethod());
}
}
Using javac 1.6.0_32-ea, this brings me the following compiler error
CompilerError.java:5: Simple cannot be inherited with different arguments: <java.lang.String> and <java.lang.Integer>
public <T extends SimpleString & SimpleInteger> void here(T interesting) {
^
1 error
这篇关于如何为通用接口类型变量再现java编译/运行时错误,这些变量可能不是同时有两个param接口的子类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!