2014-02-21 136 views
1

我有一個小模擬器,模擬狐狸和兔子... 我想讓狐狸和兔子類來實現演員類,但我得到這個錯誤消息,我不知道什麼是錯..是不是抽象的,不重寫抽象方法行爲

錯誤消息:兔子是不是抽象的,在演員不重寫抽象方法步驟(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(); 
} 

回答

4

通過實施Actor,您承諾實施void act(List<Actor> xxx)方法,但您沒有。您確實實施了方法void act(List<Animal> xx),但這是一種不同的方法(具有不同的簽名)。

簡單地說,你沒有正確重寫act方法:

public void act(List<Animal> newRabbits) != void act(List<Actor> newActors); 

簽名必須一致。由於actActor抽象(即接口),則必須將其覆蓋在實現Actor類或聲明它abstract(不能夠實例化。

+0

如果我用動作列表替換動畫列表,我得到一個錯誤兔子不是抽象的,也不會重寫動物的抽象方法動作(java.until.List ) – Emperial

+0

因爲你的動物類/接口正在實現一種不同的方法沒有實現'Actor'接口。他們都必須對應。關於爲什麼這是不允許檢查這個答案:http://stackoverflow.com/questions/2995926/why-is-there-no-parameter-contra-variance-for-overriding – Jack

+0

如果我讓動物實施演員..然後兔子找不到符號newRabbit – Emperial

0

@J ack是對的。

我想解釋爲什麼這是不允許的。 (如果它是一個數組將被允許。)

考慮動物類

public class Animal{ 
     String name; 

    //getters and setters 
} 

public class Dog extends Animal{ 

public void woof(){ } 
} 

public class Cat extends Animal{ 
    public void meow(){} 
} 

PS:以下是不允許的,但顯示只是爲了解釋爲什麼它不是允許

List<Animal> lst = new ArrayList<Animal>(); 

lst.add(new Dog()); 
lst.add(new Cat()); 

makeNoise(lst); //**** IS NOT ALLOWED BUT SIMULATES YOUR SITUATION. *****// 

你有一個方法在一些其他類是

public makeNoise(List<Dog> dogNoises){ 

//iterate over the list and do follwoing for each Dog 
//Remeber we put a cat in Dog list? Cat doesnt woof!!! it will break or crash or something else you name it. 

for(Dog doggie: dogNoises) 
    doggie.woof(); 
} 

通用集合確保我們不碰到是起訴如上。

+0

是的,我知道了...並且我更改了代碼,以便動物實現演員..但是現在,兔子類給了我一個錯誤,它無法在動作方法中找到newRabbit – Emperial

+0

您可以發佈您的編輯代碼嗎? –

+0

編輯代碼 – Emperial

相關問題