0

无法弄清楚为什么我会收到此错误。在记事本中工作,通过 cmd 运行 javac(显然更多的是文件)。如果您需要我链接更多,我可以。我的错误发生在“setCruiseShip”,我试图将当前链接到超类。错误:对 super 的调用必须是构造函数中的第一条语句。

class Ship
{ protected String name;
  protected float speed;
  protected int year;

  public Ship() {name=""; speed=year=0;}
  public Ship(String n, float sp, int y) {name=n; speed=sp; year=y;}
  public String getName()  {return name;}
  public float  getSpeed() {return speed;}
  public int    getYear()  {return year;}
  public void   setShip(String n, float sp, int y) {name=n; speed=sp; year=y;}
  public String toString() {return "Ship named "+name+ " with max speed of " 
                           +speed+ " was built in " +year;}
}

class CruiseShip extends Ship
{ protected int maxpass;

  public CruiseShip() {super(); maxpass=0;}
  public CruiseShip(String n, float sp, int y, int mp) {super(n, sp, y);
                                                        maxpass=mp;}
  public int  setMaxpass(int mp) {maxpass=mp;}
  public void setCruiseShip(String n, float sp, int y, int mp){super(n,sp,y);
                                                               maxxpass=mp;}
  public String toString() { return super.toString() +" carries "
                            +maxpass+ "  passengers";}
} 
4

3 回答 3

1

class Ship没有名为 的方法setCruiseShip。您可能想要的是调用该setShip方法。代替

super(n,sp,y);

setShip(n,sp,y);
于 2013-03-11T08:51:58.633 回答
0

只能从子类的构造函数(不能从任何方法)调用超级构造函数。下面给出修改和编译的程序

class Ship {
    protected String name;
    protected float speed;
    protected int year;

    public Ship() {
        name = "";
        speed = year = 0;
    }

    public Ship(String n, float sp, int y) {
        name = n;
        speed = sp;
        year = y;
    }

    public String getName() {
        return name;
    }

    public float getSpeed() {
        return speed;
    }

    public int getYear() {
        return year;
    }

    public void setShip(String n, float sp, int y) {
        name = n;
        speed = sp;
        year = y;
    }

    public String toString() {
        return "Ship named " + name + " with max speed of " + speed
                + " was built in " + year;
    }
}

class CruiseShip extends Ship {
    protected int maxpass;

    public CruiseShip() {
        super();
        maxpass = 0;
    }

    public CruiseShip(String n, float sp, int y, int mp) {
        super(n, sp, y);
        maxpass = mp;
    }

    public int setMaxpass(int mp) {
        maxpass = mp;
    }

    public void setCruiseShip(String n, float sp, int y, int mp) {
        setShip(n, sp, y);
        maxpass = mp;
    }

    public String toString() {
        return super.toString() + " carries " + maxpass + "  passengers";
    }
}
于 2013-03-11T09:04:51.650 回答
0

super(somevalues...)可以从子类的构造函数中调用。Java 不允许super(somevalues...)从方法调用。此外,当您在子类中定义构造函数时,其第一条语句应该是super()/super(somevalues) 构造函数或this(somevalues...). 如果你不这样做,那么编译器会隐式添加默认的无参数超级构造函数。

this() 意味着构造函数重载。
给定的事实是

public void setCruiseShip(String n, float sp, int y, int mp){super(n,sp,y);

不是子类中的构造函数。

构造函数definition是,构造函数名称必须(我不是说“应该”)与没有返回类型的类名称相同,并且允许任何访问修饰符(如私有、默认、受保护、公共)。

所以setCruiseShip不一样CruiseShip

于 2013-03-11T09:05:12.773 回答