请看以下代码,为什么不能编译呢
[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的类型,但不知道为什么这样写会出错。请各位仁兄帮手解答。
只是要编译通过的话,另一种改法是把第25行的声明类型改为List<? super Integer>。List<? super T>不是一种确定的类型,所以不能赋值给确定的泛型类型List。楼主确认一下用泛型通配符的目的……之前在[url=http://iwtxokhtd.iteye.com/blog/386833#comments]这帖[/url]回过一些,有兴趣的话可以看看~
这样你的返回类型就变成必须满足List<? super Integer>
List却不是List<? super Integer>
看看接口public interface List
这个E类型必须是确定的类型,"? super Integer"类型却不是确定的
public static [color=red]List [/color]process(List nums)
这样才行的