Java中的多态性和ArrayList
我有一个classJava中的多态性和ArrayList,java,oop,arraylist,polymorphism,Java,Oop,Arraylist,Polymorphism,我有一个class单元格和一个class邻居扩展单元格。但是,当我试图将ArrayList传递给一个需要ArrayList的函数时,会出现一个错误。我错过了什么 class Cell { PVector pos; Cell(PVector pPos) { pos = pPos.get(); } } class Neighbour extends Cell { int borders = 0; Neighbour(PVector pPo
单元格
和一个class邻居
扩展单元格
。但是,当我试图将ArrayList
传递给一个需要ArrayList
的函数时,会出现一个错误。我错过了什么
class Cell {
PVector pos;
Cell(PVector pPos) {
pos = pPos.get();
}
}
class Neighbour extends Cell {
int borders = 0;
Neighbour(PVector pPos) {
super(pPos);
}
}
private int inSet(PVector pPos, ArrayList<Cell> set) {
[...]
return -1;
}
[...]
ArrayList<Neighbour> neighbours = new ArrayList<Neighbour>();
PVector pPos = new PVector(0, 0);
[...]
inSet(pPos, neighbours);
类单元{
PVector位置;
单元(PVector-pPos){
pos=pPos.get();
}
}
类邻居扩展单元{
int-borders=0;
邻居(PVector-pPos){
超级(pPos);
}
}
专用int插图(PVector pPos、ArrayList集合){
[...]
返回-1;
}
[...]
ArrayList邻居=新的ArrayList();
PVector-pPos=新的PVector(0,0);
[...]
插图(pPos,邻居);
最后一行抛出错误`方法iniSet(PVector,ArrayList)不适用于参数(PVector,ArrayList)
谢谢你的帮助 试试:
private int inSet(PVector pPos, List<? extends Cell> set)
private int inSet(PVector-pPos,List尝试:
private int inSet(PVector pPos, List<? extends Cell> set)
private int inSet(PVector-pPos,List这是因为
List<A> != List<B> ... even if B extends A.
List!=List…即使B扩展了A。
您需要做的是将函数修改为以下内容
private int inSet(PVector pPos, ArrayList<? extends Cell> set) {
[...]
return -1;
}
private int inSet(PVector-pPos,ArrayList这是因为
List<A> != List<B> ... even if B extends A.
List!=List…即使B扩展了A。
您需要做的是将函数修改为以下内容
private int inSet(PVector pPos, ArrayList<? extends Cell> set) {
[...]
return -1;
}
private int inSet(PVector pPos,arraylist)这可能有助于理解:这可能有助于理解: