import java.util.LinkedList;
class GenericInseption{
public static void main(String[] args){
LinkedList<LinkedList<Dad>> listOfLists = new LinkedList<>();
LinkedList<Dad> list = new LinkedList<>();
listOfLists.add(list);
test(listOfLists);//error
}
private static void test(LinkedList<LinkedList<? extends Person>> listOfLists){
for(LinkedList<? extends Person> list : listOfLists){
for(Person person : list){
//peform operation on person objects
}
}
}
}
class Person{}
class Dad extends Person{}
GenericInseption.java:7: error: method test in class GenericInseption cannot be applied to given types;
test(listOfLists);//error
^
required: LinkedList<LinkedList<? extends Person>>
found: LinkedList<LinkedList<Dad>>
reason: argument mismatch; LinkedList<LinkedList<Dad>> cannot be converted to LinkedList<LinkedList<? extends Person>>
1 error
但鏈表<爸爸>是鏈表<的子類型?延伸人>
這應該如何工作?
您_would_需要'LinkedList的<?擴展LinkedList <?擴展Person >>'。 –
謝謝路易斯,這似乎工作 –
或只是'列表<?擴展List <?擴展人>> –