不抽象,不覆盖抽象方法行为

时间:2014-02-21 18:43:21

标签: java implements

我有一个模拟狐狸和兔子的小型模拟器...... 我正在尝试制作狐狸和兔子类来实现actor类,但是我得到了这个错误信息,我不知道什么是错的..

错误消息:rabbit不是抽象的,并且不会覆盖Actor中的抽象方法act(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();
}

3 个答案:

答案 0 :(得分:5)

通过实现Actor,您承诺实现void act(List<Actor> xxx)方法,但您没有。您确实实现了方法void act(List<Animal> xx),但这是一种不同的方法(具有不同的签名)。

简单地说,您没有正确覆盖act方法:

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

签名必须对应。由于act中的Actor是抽象的(作为接口),因此必须在实现Actor的类中覆盖它,或者将其声明为abstract(不能实例化它。 / p>

答案 1 :(得分:0)

接口是一种合同,您将在实现这些接口的类中提供某些功能。如果你试图创建一个具体的类,你必须从Actor接口实现act()方法。

您可以在这里阅读更多内容: http://docs.oracle.com/javase/tutorial/java/concepts/interface.html

答案 2 :(得分:0)

@Jack是对的。

我想解释为什么不允许这样做。 (如果是数组,则允许。)

考虑动物类

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();   
}

通用集合确保我们不会遇到上述问题。