实际和形式参数的长度不同 - Java构造函数错误

时间:2016-03-02 09:23:10

标签: java

这是一个模拟器,其中鸟类群体在转弯的基础上根据不同的自然事件(例如食物,繁殖等)而变化。我使用ArrayList来存储鸽子。

我不完全确定我的代码在这里出了什么问题,但编译错误是“实际和正式的参数长度不同”。如果有人能指出正确的方向,那将是值得赞赏的。

超级

public abstract class Bird
{

protected int age;

public Bird(int age)
{
age = 0; 
}
}    

子类

public class Dove extends Bird
{
private static final int BREEDING_AGE = 2;
private static final int MAX_AGE = 15;
private static final double EGG_PROBABILITY = 0.16;
private static final int MAX_NEST_SIZE = 2;
private static final Random rand = Randomizer.getRandom();

private boolean alive;
private Location location;
private Field field;

public Dove(int age, boolean randomAge, Field field, Location location)
{
    super(age);
    alive = true;
    this.field = field;
    setLocation(location);
    if(randomAge) {
        age = rand.nextInt(MAX_AGE);
    }
}

private boolean willBreed()
{
    return age >= BREEDING_AGE;
}

public void increaseAge()
{
    age++;
    if(age > MAX_AGE) {
        setDead();
    }
}

public void run(List<Dove> newDoves)
{
    increaseAge();
    if(alive) {
        giveBirth(newDoves);            
        Location newLocation = field.freeAdjacentLocation(location);
        if(newLocation != null) {
            setLocation(newLocation);
        }
        else {
            // Overcrowding.
            setDead();
        }
    }
}

public boolean stillAlive()
{
    return alive;
}

public void killBird()
{
    alive = false;
    if(location != null) {
        field.clear(location);
        location = null;
        field = null;
    }
}

public Location getLocation()
{
    return location;
}

private void setLocation(Location newLocation)
{
    if(location != null) {
        field.clear(location);
    }
    location = newLocation;
    field.place(this, newLocation);
}

private void giveBirth(List<Dove> Dove)
{
    List<Location> free = field.getFreeAdjacentLocations(location);
    int births = breed();
    for(int b = 0; b < births && free.size() > 0; b++) {
        Location loc = free.remove(0);
        Dove chick = new Dove(false, field, loc);
        newDoves.add(chick);
    }
}

private int breed()
{
    int births = 0;
    if(canBreed() && rand.nextDouble() <= EGG_PROBABILITY) {
        births = rand.nextInt(MAX_NEST_SIZE) + 1;
    }
    return births;
}
}

谢谢。

1 个答案:

答案 0 :(得分:0)

这是构造函数的签名:

public Dove(int age, boolean randomAge, Field field, Location location)

这是您尝试实例化Dove对象的方式:

Dove chick = new Dove(false, field, loc);

(之前您将变量new命名为错误,但我看到您对其进行了编辑,因此我不会参考此内容)
您正在将 3 参数传递给构造函数,如果您看到 4

要解决此问题,只需在调用中添加age参数,可能如下:

Dove chick = new Dove(10, false, field, loc);

以上内容将使用Dove创建age = 10