gpt4 book ai didi

java - 构造函数调用给出错误继承

转载 作者:行者123 更新时间:2023-12-01 11:44:22 26 4
gpt4 key购买 nike

在子类GreenSlime中,我给出了一个只有三个参数的构造函数(我无法添加任何其他实例变量)。但代码不断给出有关此行的错误:super(loc,map,log);,我知道构造函数应该具有相同数量的参数。但我的规范说通过父构造函数设置所有字段。 fullcharge 必须始终为 4charge 的起始值为 0。我确实知道我只传递了 3 个参数,而不是 5 个,但我的项目说明是这么说的。我做错了什么以及最好的方法/解决方案是什么?

import java.io.PrintStream;
public class GreenSlime extends Threat {

public GreenSlime(Coord loc, Map map, PrintStream log)
{
super(loc,map,log);
super.fullCharge = 4;
super.charge = 0;
}

}
import java.io.PrintStream;



public abstract class Threat extends Thing {

protected int charge;
protected final int fullCharge;

public Threat(Coord c, String repr, int fullCharge, Map map, PrintStream log)
{
super(c,repr,map,log);
this.fullCharge = fullCharge;
charge = 0;
}

public abstract void spawn(Coord c);

@Override
public void doAction()
{

while(charge != fullCharge)
{
System.out.println("\"+repr()"+"@"+"getLoc()\" speading");

if(this.canPassThrough())
{
spawn(getPrevLoc().step(Direction.N));
spawn(getPrevLoc().step(Direction.S));
spawn(getPrevLoc().step(Direction.E));
spawn(getPrevLoc().step(Direction.W));
}
charge++;
}
}

}

最佳答案

public GreenSlime(Coord loc, Map map, PrintStream log)
{
super(loc,"",4,map,log);
}

我为 repr 提供了一个空字符串“”,但您可能需要 null 或其他值。

关于java - 构造函数调用给出错误继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29272355/

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