0

我有一个类Cell和一个类Neighbour扩展Cell。但是当我尝试将 an 传递ArrayList<Neighbour>给期望ArrayList<Cell>. 我错过了什么?

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);

最后一行抛出错误`The method iniSet(PVector, ArrayList) is not applicable for the arguments (PVector, ArrayList);

谢谢你的帮助!

4

2 回答 2

3

那是因为

List<A> != List<B> ... even if B extends A.

您需要做的是将功能修改为以下

private int inSet(PVector pPos, ArrayList<? extends Cell> set) {
    [...]
    return -1;
}

希望有帮助。

于 2013-11-08T07:53:18.087 回答
2

尝试:

private int inSet(PVector pPos, List<? extends Cell> set)
于 2013-11-08T07:52:46.113 回答