gzllyh 2009-06-21 21:48
浏览 175
已采纳

java泛型问题

请看以下代码,为什么不能编译呢
[code="java"]
import java.util.*;
public class GenerClass {

/**
 * Creates a new instance of <code>GenerClass</code>.
 */
public GenerClass() {
}



//just for testing
public static <E extends Number> List<? super E> process(List<E> nums)
{
    return null;
}


/**
 * @param args the command line arguments
 */
public static void main(String[] args) {

  List<Integer> input = new ArrayList<Integer>(0);
  List<Integer> output = process(input);  


}

}[/code]

编译的时候出错,说不兼容的类型。问题应该出自output的类型,但不知道为什么这样写会出错。请各位仁兄帮手解答。

  • 写回答

2条回答 默认 最新

  • rednaxelafx 2009-06-21 22:45
    关注

    只是要编译通过的话,另一种改法是把第25行的声明类型改为List<? super Integer>。List<? super T>不是一种确定的类型,所以不能赋值给确定的泛型类型List。楼主确认一下用泛型通配符的目的……之前在[url=http://iwtxokhtd.iteye.com/blog/386833#comments]这帖[/url]回过一些,有兴趣的话可以看看~

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?