简体   繁体   English

java 和 inheritance 构造函数

[英]java and inheritance constructors

I have a class called Item done in the following way我有一个名为 Item 的 class 通过以下方式完成

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();
}
}

My problem is that I have to implement two classes one of them inherits from Items, from which they ask me: this class only has a constructor and it will be with parameters.我的问题是我必须实现两个类,其中一个从 Items 继承,他们问我:这个 class 只有一个构造函数,它将带有参数。 The order of these will be: xCoord, yCoord, length, height, energy and tank.它们的顺序是:xCoord、yCoord、length、height、energy 和 tank。 The value of spriteImage will always be the same: "./images/food/seed.png" I implemented it as follows 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");
}

The other class is the same, they ask me that this class will have a single constructor and will be with parameters, the order of which will be: xCoord, yCoord, length, height and tank.另一个 class 也是一样,他们问我这个 class 将有一个构造函数,并且将带有参数,其顺序为:xCoord、yCoord、长度、高度和坦克。 The value of spriteImage will always be the same: "./images/submarine/submarine.png" 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");     
}

My problem is that I can't get the constructors that ask me in both classes to have only the parameters that are told to me.我的问题是我无法让在两个类中都要求我只拥有告诉我的参数的构造函数。

The constructor, depending on the superclass, always creates for me in Super () the parameters that come from Item, but they ask me to only have the parameters that they ask me for, that would imply removing "spriteImage" from the constructor of the superclass.构造函数,取决于超类,总是在 Super() 中为我创建来自 Item 的参数,但他们要求我只拥有他们要求的参数,这意味着从构造函数中删除“spriteImage”超类。

Some way?某种方式?

Couldn't the item class be abstract?项目 class 不能是抽象的吗?

You can simply omit spriteImage from the childrens' ctors.您可以简单地从孩子的 ctors 中省略 spriteImage。 Result should be something like this:结果应该是这样的:

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);
   }
}

You could also ignore the spriteImage param and call super() with a constant String:您也可以忽略 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);
}

we might then remove the spriteImage -parameter:然后我们可能会删除spriteImage参数:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM