扩展类的Eclipse错误

时间:2013-03-22 20:48:12

标签: java libgdx

我正在尝试从下面的那个创建和扩展类,但是我从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;

}

有没有更好的方法来设置具有相同变量和操作但具有不同值的“船只”?

2 个答案:

答案 0 :(得分:3)

您似乎正在尝试为Frigate初始化实例变量。将该代码放在构造函数中。

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

答案 1 :(得分: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抽象化。