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

我将创建一个接受两个值的构造函数 fir Ship:

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

然后从子类调用它:

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

如果它只是一个“基”类,您也应该考虑使 Ship 抽象。

于 2013-03-22T21:13:22.627 回答