我有一个方法,需要一个超类(特别是我的类Mob)的列表。它看起来像这样:将子类对象添加到超类列表
List<? extends Mob> mobs
我希望能够加入扩展了超类的群氓到这个列表中的任何对象,像这样:
spawnMob(new Zombie(World.getInstance(), 0, 0), World.getInstance().getZombies());
如果是这种问题的方法:
public static void spawnMob(Mob mob, List<? extends Mob> mobs){
mobs.add(mob);
}
这行代码World.getInstance().getZombies()
返回对象Zombie的列表。僵尸延伸暴民。
然而,这行代码:
mobs.add(mob);
抛出这个错误:
The method add(capture#1-of ? extends Mob) in the type List<capture#1-of ? extends Mob> is not applicable for the arguments (Mob)
我能做些什么来解决这个问题?
编辑,改变以除外List<Mob>
的方法后,我收到此错误:
The method spawnMob(Mob, List<Mob>) in the type MobSpawner is not applicable for the arguments (Zombie, List<Zombie>)
为什么不定义你的列表,列表' mobs'?您仍然可以将扩展Mob的对象添加到该列表中。 –
exception1