问题描述
<$是否有任何方法可以使用由封闭类声明的类型变量作为在内部类中声明的类型变量的边界? c $ c> class Test< E> {
class Inner< T extends E> {}
< T扩展E> void doStuff(T arg){}
public static void main(String [] args){
new Test< Number>()。doStuff(new Integer(0)); //正常工作
new Test< Number>()。new Inner< Integer>(); //不会编译
}
}
javac给出此错误:
Test.java:6:类型参数java.lang.Integer不在其绑定范围内
new Test< Number> ;()。new Inner< Integer>();
^
我无法找到满足编译器的任何类型组合。 Inner
与 doStuff $ c声明的类型参数
T
之间有什么区别$ C>?为什么一个人工作,另一个人不工作?
我不想寻找替代品,我只想更好地理解语言是如何工作的。 / p>
错误ID:6557954
投票2
概要检查类型格式良好时,内部类类型参数不会被替换
类别java:编译器
版本固定7(b40)
状态10-Fix已交付,bug
优先级:5非常低
提交日期2007年5月16日
发布日期:2008-07-02 16:22:46.0
描述
class Foo< T> {
class Bar< U extends T> {}
Foo< Number> .Bar< Integer> F;
}
评估
Number<:Object
Integer<:[Number / T] T = Number
整数<:T
编辑:
在我的系统中,问题中的代码编译时没有错误7 javac
:
C:\workspace\Sandbox\ src>%JAVA_HOME%\bin\javac.exe-version
javac 1.7.0 -ea
但是它失败了,因为Java 6 javac
的问题中显示错误:
C:\workspace\Sandbox\rcrc>%JAVA_HOME%\bin\javac.exe-version
javac 1.6.0_17
Is there any way to use a type variable declared by an enclosing class as a bound on a type variable declared in an inner class?
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 gives this error:
Test.java:6: type parameter java.lang.Integer is not within its bound
new Test<Number>().new Inner<Integer>();
^
I can't find any combination of types that will satisfy the compiler. What's the difference between the type parameter T
as declared by Inner
versus doStuff
? Why does one work and the other doesn't?
I'm not looking for an alternative, I just want to gain a better understanding of how the language works.
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
Description
class Foo<T> {
class Bar<U extends T> {}
Foo<Number>.Bar<Integer> f;
}
Evaluation
Number <: Object
Integer <: [Number/T]T = Number
Integer <: T
Edit:
On my system, the code in the question compiles without error with Java 7 javac
:
C:\workspace\Sandbox\src>"%JAVA_HOME%\bin\javac.exe" -version
javac 1.7.0-ea
But it fails with the error indicated in the question for Java 6 javac
:
C:\workspace\Sandbox\src>"%JAVA_HOME%\bin\javac.exe" -version
javac 1.6.0_17
这篇关于通过包含类类型变量绑定的内部类类型参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!