gpt4 book ai didi

java - 不抽象且不重写抽象方法行为

转载 作者:行者123 更新时间:2023-11-29 07:49:58 25 4
gpt4 key购买 nike

我有一个模拟狐狸和兔子的小模拟器...我正在尝试制作 fox 和 rabbit 类来实现 actor 类,但我收到此错误消息,我不知道出了什么问题..

错误信息:rabbit Is not abstract and does not override abstract method act (java.until.List) in Actor

兔子类

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

}

Actor 类

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

最佳答案

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

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

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

签名必须对应。自 actActor 中是抽象的(作为接口(interface))您必须在实现 Actor 的类中重写它或者声明它abstract (无法实例化它。

关于java - 不抽象且不重写抽象方法行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21942650/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com