Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java中的多态性和ArrayList_Java_Oop_Arraylist_Polymorphism - Fatal编程技术网

Java中的多态性和ArrayList

Java中的多态性和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
单元格
和一个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)这可能有助于理解:这可能有助于理解: