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;

}

同じ変数とアクションで異なる値を持つ「船」を設定するより良い方法はありますか?

4

2 に答える 2

3

のインスタンス変数を初期化しようとしているようですFrigate。そのコードをコンストラクター内に配置します。

public Frigate()
{
    this.hull = 10;
    this.shield = 10;
}
于 2013-03-22T20:51:22.607 に答える
0

2 つの値を受け入れる Ship コンストラクターを作成します。

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

次に、サブクラスから呼び出します。

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

Ship が単なる「基本」クラスである場合は、Ship も抽象化することを検討する必要があります。

于 2013-03-22T21:13:22.627 に答える