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