java 和继承构造函数

java and inheritance constructors

我有一个 class 名为 Item 的方法

public class Item {


private static int nextId = 0;
private double xCoord, yCoord; //location
private double length, height; // define size of item
private String spriteImage;
private Tank tank;
private String id;


protected Item(double xCoord, double yCoord, String spriteImage, double length, double height,  Tank tank) throws ItemException {
    setId("I"+nextId); 
    nextId++;   
    setLocation(xCoord,yCoord);
    setSpriteImage(spriteImage);        
    setLength(length);
    setHeight(height);      
    setTank(tank);
}


/**
 * Set this item's location.
 * 
 * @param xCoord the column coordinate.
 * @param yCoord the row coordinate.
 */
public void setLocation(double xCoord, double yCoord) {
    this.xCoord = xCoord;
    this.yCoord = yCoord;
}

public double getXCoord() {
    return xCoord;
}

public double getYCoord() {
    return yCoord;
}

public double getLength() {
    return length;
}

public void setLength(double length) throws ItemException{
    if(length<=0) {
        throw new ItemException("MSG_ERR_LENGTH_VALUE");
    }
    this.length = length;
}

public double getHeight() {
    return height;
}

public void setHeight(double height) throws ItemException{
    if(height<=0) {
        throw new ItemException("MSG_ERR_HEIGHT_VALUE");
    }
    this.height = height;
}

public void setSpriteImage(String spriteImage) {

    this.spriteImage = spriteImage;
}

public String getSpriteImage() {        
    return spriteImage;
}

public String getId() {
    return id;
}

protected void setId(String id) {
    this.id = id;
}

public Tank getTank() {
    return tank;
}

public void setTank(Tank tank){
    if(tank!=null) {
        if(!tank.getItems().contains(this)) {
                tank.getItems().add(this);
        }
    }

    if(this.tank!=null) {
        try{
            this.tank.removeItem(this);
        }catch(Exception e) {
            //removeItem: El item no existe ya en el tanque
        }
    }

    this.tank = tank;
}


@Override
public String toString() {
    StringBuilder str = new StringBuilder("(" + getXCoord() +",  "+ getYCoord() +") ");

    str.append(getId() + " ");
    str.append(getLength() + " ");
    str.append(getHeight() + " ");
    str.append((getTank()!=null)?getTank().getName():"No tank");
    return str.toString();
}
}

我的问题是我必须实现两个 classes,其中一个继承自 Items,他们从 Items 中问我:这个 class 只有一个构造函数,它将与 参数。这些的顺序将是:xCoord,yCoord,长度, 高度,能量和坦克。 spriteImage 的值永远是 相同:“./images/food/seed.png” 我是这样实现的

public class Food extends Item {

double speed=1;
boolean eaten;
int energy;


protected Food(double xCoord, double yCoord, String spriteImage,  double length, double height,int energy, Tank tank)
        throws Exception {
    super(xCoord, yCoord, spriteImage, length, height, tank);   
    setSpeed(speed);
    setEnergy(energy);
    setSpriteImage("./images/food/seed.png");
}

另一个class是一样的,他们问我这个class会有一个构造函数并且会是 带有参数,其顺序为:xCoord、yCoord、 长度,高度和坦克。 spriteImage 的值永远是 相同:“./images/submarine/submarine.png”

public class SubmarineToys extends Item{


double speed=1;
boolean facingRight=true;
double thresholdReverse=0.0003;

protected SubmarineToys(double xCoord, double yCoord, String spriteImage, double length, double height, Tank tank)
        throws ItemException {
    super(xCoord, yCoord, spriteImage, length, height, tank);
    setSpriteImage("./images/submarine/submarine.png");     
}

我的问题是,我无法让在两个 classes 中询问我的构造函数只具有告诉我的参数。

构造函数,依赖superclass,总是在Super()中为我创建来自Item的参数,但是他们让我只拥有他们要求我的参数,那会暗示从 superclass.

的构造函数中删除 "spriteImage"

有办法吗?

项目 class 不能是抽象的吗?

您可以简单地省略儿童医生的精灵图像。 结果应该是这样的:

public class Food extends Item {
   private static String foodSpriteImage = "./images/food/seed.png";
   private double speed=1;
   private boolean eaten;
   private int energy;

   // Child Ctor needs to be public.
   public Food(double xCoord, 
                  double yCoord, 
                  double length, 
                  double height,
                  int energy, 
                  Tank tank)
        throws Exception {
       super(xCoord, yCoord, foodSpriteImage , length, height, tank);   
       setEnergy(energy);
   }
}

您也可以忽略 spriteImage 参数并使用常量字符串调用 super()

protected Food(double xCoord, double yCoord, String spriteImage, double length, double height, int energy, Tank tank) throws Exception {
    super(xCoord, yCoord, "./images/food/seed.png", length, height, tank);
    //                         ↑ here we put our default value instead of the spriteImage parameter
    setSpeed(speed);
    setEnergy(energy);
}

然后我们可能会删除 spriteImage 参数:

protected Food(double xCoord, double yCoord, double length, double height, int energy, Tank tank) {...}