通过包含类类型变量绑定的内部类类型参数 [英] Inner class type parameter bound by enclosing class type variable

查看:88
本文介绍了通过包含类类型变量绑定的内部类类型参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <$是否有任何方法可以使用由封闭类声明的类型变量作为在内部类中声明的类型变量的边界? 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 T 之间有什么区别$ C>?为什么一个人工作,另一个人不工作?



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

解决方案

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

 错误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;
}

评估


这是Check.java中的一个问题,因为在检查绑定一致性时,实际类型参数仅在最上面的类型变量的绑定中被取消。在这种情况下,我们已经将Foo.Bar与实际的类型参数T = Number,U = Integer进行核对。所以应该是这样的:




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




不幸的是,javac错过了第二次替换, :



 整数<: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

The compiler fails to accept this program:

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

Evaluation

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.

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

这篇关于通过包含类类型变量绑定的内部类类型参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆