【发布时间】:2012-08-30 15:14:45
【问题描述】:
我有这门课
public class Tree<T> {
//List of branches for this tree
private List<Tree<? super T>> branch = new ArrayList<Tree<? super T>>();
public Tree(T t){ this.t = t; }
public void addBranch(Tree< ? super T> src){ branch.add(src); }
public Tree<? extends T> getBranch(int branchNum){
return (Tree<? extends T>) branch.get(branchNum);
}
private T t;
}
我正在尝试使用这个从这个类中创建一个变量
public static void main(String[] args){
Tree<? super Number> num2 = new Tree<? super Number>(2);
}
它给了我这个错误
Cannot instantiate the type Tree<? super Number>
【问题讨论】:
-
嗯,这不是完全重复的,因为那是 Java 7 的问题。
-
该帖子上的 OP 询问编译错误 - 误解是一样的。
标签: java generics wildcard bounded-wildcard