2011-01-27 99 views
3

是否有任何方式使用由包含类声明的类型变量作为在内部类中声明的类型变量的绑定?通过包含类类型变量绑定的内部类类型参数

class Test<E> { 
    class Inner<T extends E> {} 
    <T extends E> void doStuff(T arg) {} 
    public static void main(String[] args) { 
     new Test<Number>().doStuff(new Integer(0)); // works fine, as expected 
     new Test<Number>().new Inner<Integer>(); // won't compile 
    } 
} 

的javac给出了这样的错误:

Test.java:6: type parameter java.lang.Integer is not within its bound 
      new Test<Number>().new Inner<Integer>(); 
              ^

我无法找到的类型,将满足编译器的任意组合。 InnerdoStuff所声明的类型参数T之间有什么区别?为什么一个人工作,另一个人不工作?

我不是在寻找替代品,我只是想更好地理解语言是如何工作的。

+1

该示例编写并运行良好,如我所写。 – ILMTitan 2011-01-27 23:55:04

+1

@ILMTitan你在用什么编译器? – gdejohn 2011-01-27 23:59:09

回答

3

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6557954

Bug ID: 6557954 
Votes 2 
Synopsis Inner class type parameters doesn't get substituted when checking type well-formedness 
Category java:compiler 
Release Fixed 7(b40) 
State 10-Fix Delivered, bug 
Priority: 5-Very Low 
Submit Date 16-MAY-2007 
Posted Date : 2008-07-02 16:22:46.0 

说明

The compiler fails to accept this program:

class Foo<T> { 
    class Bar<U extends T> {} 
    Foo<Number>.Bar<Integer> f; 
} 

评价

This is a problem in Check.java as when checking for bound-conformance actual type parameters are subsituted only in topmost type-variable's bound. In this case we have that Foo.Bar is to be checked against the actual type-parameters T=Number, U=Integer

So it should be the case that:

Number <: Object 
Integer <: [Number/T]T = Number 

unfortunately, javac misses the second substitution so that the check becomes:

Integer <: T 

which is wrong and cause the error.

编辑:

在我的系统,在该问题的代码没有错误编译与Java 7 javac

C:\workspace\Sandbox\src>"%JAVA_HOME%\bin\javac.exe" -version 
javac 1.7.0-ea 

但它失败,出现错误在Java 6 javac的问题中指出:

C:\workspace\Sandbox\src>"%JAVA_HOME%\bin\javac.exe" -version 
javac 1.6.0_17