Pong Game:Walls和Balls几乎完全相同 - 如何超类Ellipse2D和Rectangle2D?

时间:2015-09-07 01:56:17

标签: java inheritance superclass

我正处于学习Java的早期阶段,我只是想制作一个简单的Pong类型的游戏。我有两个类Wall和Ball。它们几乎是相同的,除了它们各自有一个不同的java.awt.geom sublcass(一个有Ellipse2d和一个Rectangle2D)但几乎所有其他方式它们是相同的(Ball类将有一些更多的变量处理速度,但在其他方面相同)。

我的问题是,现在我发现有冗余我想创建一个超类GameObject。我只是不确定如何处理这两个子类引用Ellipse2D和Rectangle 2D的事实

类Wall {

private double posX, posY, width, height; // wall position and dimensions
Rectangle2D wall2D;

/**
 * 
 */
public Wall(double Xin, double Yin, double widthIn, double heightIn) { // passes in variables and creates a new Ellipse2D
    posX = Xin;
    posY = Yin;
    width = widthIn;
    height = heightIn;
    wall2D = new Rectangle2D.Double(posX, posY, width, height);
}

private void setWall2D() { // makes the Wall2D have the same location/dimensions as the variables in this class
    wall2D.setFrame(posX, posY, width, height);
}

Rectangle2D getWall2D() { // returns the actual Rectangle2D so it can get displayed on the screen when needed
    return wall2D;
}

// getters
double getPositionX() {
    return posX;
}

double getPositionY() {
    return posY;
}

double getHeight() {
    return height;
}

double getWidth() {
    return width;
}

// setters (all have to call setWall2D after variable has been set! - to update the actual Rectangle2D shape)
void setPositionX(double xIn) {
    posX = xIn;
    setWall2D();
}

void setPositionY(double yIn) {
    posY = yIn;
    setWall2D();
}

void setHeight(double heightIn) {
    height = heightIn;
    setWall2D();
}

void setwidth(double widthIn) {
    width = widthIn;
    setWall2D();
}

}

这是我的球类

    class Ball {
    private double posX, posY, width, height; // ball position and dimensions
    Ellipse2D ball2D;

    /**
     * 
     */
    public Ball(double Xin, double Yin, double widthIn, double heightIn) { // passes in variables and creates a new Ellipse2D
        posX = Xin;
        posY = Yin;
        width = widthIn;
        height = heightIn;
        ball2D = new Ellipse2D.Double(posX, posY, width, height);
    }

    private void setBall2D() { // makes the Ball2D have the same location/dimensions as the variables in this class
        ball2D.setFrame(posX, posY, width, height);
    }

    Ellipse2D getBall2D() { // returns the actual Ellipse2D so it can get displayed on the screen when needed
        return ball2D;
    }

    // getters
    double getPositionX() {
        return posX;
    }

    double getPositionY() {
        return posY;
    }

    double getHeight() {
        return height;
    }

    double getWidth() {
        return width;
    }

    // setters (all have to call setBall2D after variable has been set! - to update the actual Ellipse2D shape)
    void setPositionX(double xIn) {
        posX = xIn;
        setBall2D();
    }

    void setPositionY(double yIn) {
        posY = yIn;
        setBall2D();
    }

    void setHeight(double heightIn) {
        height = heightIn;
        setBall2D();
    }

    void setwidth(double widthIn) {
        width = widthIn;
        setBall2D();
    }

}

任何建议将不胜感激。提前谢谢。

1 个答案:

答案 0 :(得分:0)

RectangularShape存储在公共超类而不是Ellipse2D / Rectangle2D中?您仍然可以在相应的构造函数中分配正确的子类...

class GameObject {
  RectangularShape shape;
  ...
}

class Ball extends GameObject {
  public Ball(double Xin, double Yin, double widthIn, double heightIn) {
    // passes in variables and creates a new Ellipse2D
    posX = Xin;
    posY = Yin;
    width = widthIn;
    height = heightIn;
    shape = new Ellipse2D.Double(posX, posY, width, height);
  }