我有一個小模擬器,模擬狐狸和兔子... 我想讓狐狸和兔子類來實現演員類,但我得到這個錯誤消息,我不知道什麼是錯..是不是抽象的,不重寫抽象方法行爲
錯誤消息:兔子是不是抽象的,在演員不重寫抽象方法步驟(java.until.List)
兔類
import java.util.List;
import java.util.Random;
public class Rabbit extends Animal implements Actor
{
// Characteristics shared by all rabbits (static fields).
// The age at which a rabbit can start to breed.
private static final int BREEDING_AGE = 5;
// The age to which a rabbit can live.
private static final int MAX_AGE = 40;
// The likelihood of a rabbit breeding.
private static final double BREEDING_PROBABILITY = 0.15;
// The maximum number of births.
private static final int MAX_LITTER_SIZE = 4;
/**
* Create a new rabbit. A rabbit may be created with age
* zero (a new born) or with a random age.
*
* @param randomAge If true, the rabbit will have a random age.
* @param field The field currently occupied.
* @param location The location within the field.
*/
public Rabbit(boolean randomAge, Field field, Location location)
{
super(field, location);
if(randomAge) {
setAge(rand.nextInt(MAX_AGE));
}
}
/**
* This is what the rabbit does most of the time - it runs
* around. Sometimes it will breed or die of old age.
* @param newRabbits A list to add newly born rabbits to.
*/
public void act(List<Actor> newActors)
{
incrementAge();
if(isActive()) {
giveBirth(newRabbits);
// Try to move into a free location.
Location newLocation = getField().freeAdjacentLocation(getLocation());
if(newLocation != null) {
setLocation(newLocation);
}
else {
// Overcrowding.
setDead();
}
}
}
public Animal getNewAnimal(boolean status, Field field, Location loc)
{
return new Rabbit(status, field, loc);
}
/**
* Return the maximal age of the rabbit.
* @return The maximal age of the rabbit.
*/
protected int getMaxAge()
{
return MAX_AGE;
}
/**
* Return the breeding age of the rabbit.
* @return The breeding age of the rabbit.
*/
protected int getBreedingAge()
{
return BREEDING_AGE;
}
/**
* Return the breeding probability of the rabbit.
* @return The breeding probability of the rabbit.
*/
protected double getBreedingProbability()
{
return BREEDING_PROBABILITY;
}
/**
* Return the maximal litter size of the rabbit.
* @return The maximal litter size of the rabbit.
*/
protected int getMaxLitterSize()
{
return MAX_LITTER_SIZE;
}
}
演員類
import java.util.List;
/**
* Write a description of interface Actor here.
*
* @author (your name)
* @version (a version number or a date)
*/
public interface Actor
{
/**
* performs actor's regular behaviour
*/
void act(List<Actor> newActors);
/**
* is the actor still active
*/
boolean isActive();
}
如果我用動作列表替換動畫列表,我得到一個錯誤兔子不是抽象的,也不會重寫動物的抽象方法動作(java.until.List) –
Emperial
因爲你的動物類/接口正在實現一種不同的方法沒有實現'Actor'接口。他們都必須對應。關於爲什麼這是不允許檢查這個答案:http://stackoverflow.com/questions/2995926/why-is-there-no-parameter-contra-variance-for-overriding – Jack
如果我讓動物實施演員..然後兔子找不到符號newRabbit – Emperial