2013-03-22 93 views
1

我試圖從一個波紋管創建和擴展類,但我發現從日食說{「令牌語法錯誤‘的錯誤消息的Eclipse錯誤用於擴展類

’,{此令牌後,預計」
import com.badlogic.gdx.graphics.Texture; 
import com.badlogic.gdx.math.Vector2; 

public class Ship { 

protected int hull; 
protected int shield; 
protected Vector2 velocity; 
protected int energy; 
protected Texture shipTexture; 
protected Vector2 position; 

public Texture getShipTexture() { 
    return shipTexture; 
} 
public void setShipTexture(Texture shipTexture) { 
    this.shipTexture = shipTexture; 
} 

public Vector2 getPosition() { 
    return position; 
} 
public void setPosition(Vector2 position) { 
    this.position = position; 
} 
public int getHull() { 
    return hull; 
} 
public void setHull(int hull) { 
    this.hull = hull; 
} 
public int getShield() { 
    return shield; 
} 
public void setShield(int shield) { 
    this.shield = shield; 
} 
public Vector2 getVelocity() { 
    return velocity; 
} 
public void setVelocity(Vector2 velocity) { 
    this.velocity = velocity; 
} 
public int getEnergy() { 
    return energy; 
} 
public void setEnergy(int energy) { 
    this.energy = energy; 
} 




} 

第一個括號之後這個類:

public class Frigate extends Ship { 

this.hull = 10; 
this.shield = 10; 

} 

有沒有更好的方式來設立「船」具有相同的變量和行動,但使用不同的值?

回答

3

它看起來像你試圖爲你的Frigate初始化實例變量。將該代碼放入構造函數中。

public Frigate() 
{ 
    this.hull = 10; 
    this.shield = 10; 
} 
+0

這看起來是一個很好的解決方案。有沒有簡單的方法來解釋爲什麼它需要構造函數來更改這些變量? – Bryan 2013-03-22 20:56:58

+1

像'this.hull = 10;'這樣的東西是語句,而不是聲明。語句需要大量代碼,例如構造函數和方法。 – rgettman 2013-03-22 20:59:01

+0

好的。感謝信息rgetttman。 – Bryan 2013-03-22 21:01:14

0

我會創建一個接受兩個值構造杉船舶:

protected Ship(int hull, int shield) { 
    this.hull = hull; 
    this.shield = shield; 
} 

然後從子類調用它:

public class Frigate extends Ship { 
    public Frigate { 
     super(10, 10); 
    } 

你應該考慮船舶抽象的爲好,如果它只是一個「基礎」課程。