【发布时间】:2016-01-13 14:54:36
【问题描述】:
我介绍了一个抽象的actor类,它是抽象动物类的子类。我现在面临的问题是 Rabbit 类(动物的子类)中的 act 方法不起作用。
我收到以下错误
“Rabbit 不是抽象的,不会覆盖 Animal 中的抽象方法 act(java.util.List<Actor>)”
我认为 Fox 中 rabbit 中的 act 方法会覆盖 Animal 和 Actor 中的 act 方法。我该如何解决这个问题?
下面是Actor类中的抽象方法
abstract public void act(List<Actor>newActors);
然后用下面的代码在 Animal 类中重写此方法
abstract public void act(List<Actor> newAnimals);
然后在 rabbit 和 fox 类中使用以下方法覆盖此方法,这就是错误出现的地方。
public void act(List<Animal> newRabbits)
{
incrementAge();
if(isAlive()) {
giveBirth(newRabbits);
// Try to move into a free location.
Location newLocation = getField().freeAdjacentLocation(getLocation());
if(newLocation != null) {
setLocation(newLocation);
}
else {
// Overcrowding.
setDead();
}
}
}
【问题讨论】:
标签: java generics inheritance type-erasure