【问题标题】:Is not abstract and does not override abstract method act不是抽象的并且不覆盖抽象方法行为
【发布时间】:2014-02-21 18:43:20
【问题描述】:

我有一个模拟狐狸和兔子的小型模拟器... 我正在尝试制作 fox 和 rabbit 类来实现 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();
}

【问题讨论】:

    标签: java implements


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 如果我用演员列表替换动物列表,我得到一个错误兔子不是抽象的,并且没有覆盖动物中的抽象方法行为(java.until.List
    • 因为你的动物类/接口在没有实现Actor 接口的情况下实现了不同的方法。它们都必须对应。关于为什么不允许这样做检查这个答案:stackoverflow.com/questions/2995926/…
    • 如果我制作动物工具 Actor.. 那么兔子找不到符号 newRabbit
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      @Jack 是对的。

      我想解释一下为什么不允许这样做。 (如果是 Array 则允许。)

      考虑动物类

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

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

      【讨论】:

      • 是的,我明白了......我更改了代码,以便动物实现演员......但现在兔子类给了我一个错误,它无法在 act 方法中找到 newRabbit
      • 好的。因此,在您的 act() 中,您需要将 newRabbits 更改为 newActors。因为您更改了参数名称。您可能需要将 giveBirth() 的签名更改为 List.
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-20
      • 2014-01-16
      • 1970-01-01
      相关资源
      最近更新 更多