java对象的层次结构,并通过对象的功能
问题描述:
我创建了一个TreeMap像这样:java对象的层次结构,并通过对象的功能
TreeMap<Integer, ArrayList<MyClass>> wrap = new TreeMap<Integer, ArrayList<MyClass>>();
我创建了一个构造函数,像这样:
public foo (TreeMap<Integer, Collection<Spot> > objects) {
this.field = objects;
}
然而,日食给了我一个红色squigly当我使用的构造,与我wrap
变量作为一个参数:
The constructor foo(TreeMap<Integer,ArrayList<Spot>>) is undefined
一个ArrayList是TY收集...是的?那么,为什么这不起作用?
答
泛型不像你认为的那样工作在这种情况下。
你需要的是类似于:
public foo (TreeMap<Integer, ? extends Collection<Spot> > objects) {
this.field = objects;
}
的?
被称为外卡。它可以让你通过Collection
,或任何延伸/实现Collection
。
线? extends Collection<Spot>
倒像是:扩展集合
东西。