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条)

报告相同问题?

悬赏问题

  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 luckysheet
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱