2013-03-11 61 views
0

第一條語句不能找出爲什麼我得到這個錯誤。在記事本中工作,通過cmd運行javac(顯然更多的是文件)。如果你需要我連接更多我可以。 我的錯誤發生在「setCruiseShip」,我試圖將當前連接到超類。 錯誤:調用超級必須是構造函數中的第一條語句。錯誤:超級必須在構造函數

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";} 
} 
+0

使用單獨的.java文件相同,也使公衆班班。 – codeMan 2013-03-11 08:49:14

+3

'setCruiseShip'不是構造函數,所以它不能調用'super'構造函數。 – 2013-03-11 08:49:20

回答

1

class Ship沒有名爲setCruiseShip的方法。你可能想要的是調用setShip方法。更換

super(n,sp,y); 

setShip(n,sp,y); 
+0

非常感謝你 – user2155992 2013-03-11 08:53:20

+0

@ user2155992如果是正確的解決方案,請將其標記爲正確答案 – uba 2013-03-11 08:57:00

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"; 
    } 
} 
+0

非常感謝! – user2155992 2013-03-11 09:08:17

+0

如果您對答案滿意,請通過單擊「勾號」將其選爲答案。 – Joe2013 2013-03-11 09:11:12

0

super(somevalues...)修改和編譯的程序給出可以從子類的構造函數被調用。 Java不允許從方法調用super(somevalues...)。當你在子類中定義構造函數時,它的第一個語句應該是super()/super(somevalues)構造函數或this(somevalues...)。如果你不這樣做,那麼編譯器會隱式添加默認的no-arg超級構造函數。

this()表示構造函數重載。
和給定的事實是

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

是不是在子類的構造函數。

構造函數definition是,構造函數名必須(我不是說'應該')與沒有返回類型的類名相同,並且允許任何訪問修飾符(如private,default,protected,public)。

所以setCruiseShip不爲兩個類CruiseShip

相關問題