如何将对象附加到接口数组处理

How to append Object to Interface Array processing

interface Creature {
   void display();
   void damage(int x);
}
class Beaver implements Creature {
    ....
}
class World {
    Creature[] creatures = new Creature[0];
    ...
    void spawnCreature(Creature c) {
        creatures = append(c, creatures);
    }
}

但是我得到一个错误:

无法从对象转换为 Skyland.Creature[]

请帮忙

此类问题最好通过查看 the Processing reference 来回答。

append() 函数有两个参数:一个数组和一个值,顺序为。你给它一个值和一个数组。

换句话说,这一行:

creatures = append(c, creatures);

应该是这样的:

creatures = append(creatures, c);

如果您仍然遇到问题,请 post 在新问题 post 中回答 MCVE。祝你好运。