-1

好的,所以我有一堆类,我想旋转一个具有对象形状的对象,它是一个二维数组,并且在对象类的父类中构造。我想更改方法 rotate() 中的属性(形状),该方法调用方法 multiplyarray 通过更改形状进行旋转。然而,我意识到我无法使用形状,或者至少我不知道如何改变它。我不认为父类有一个公共的 setShape 方法。无论如何,这是代码:

public static int[][] multiplyMatrix(int[][] m1) {

    int[][] m2 = { { 0, 0, 0, 1 }, { 0, 0, 1, 0 }, { 0, 1, 0, 0 },
            { 1, 0, 0, 0 }, };

    int[][] result = new int[4][4];

    // multiply
    for (int i = 0; i < 4; i++)
        for (int j = 0; j < 4; j++)
            for (int k = 0; k < 4; k++)
                if (m1[i][k] * m2[k][j] > 0) {
                    result[i][j] = 1;
                } else {
                    result[i][j] = 0;
                }

    return result;
}

旋转方法:

synchronized void rotateClockwise() {
    currentPiece.shape = multiplyMatrix(shape);

    //gives me an error
    updateLocation();
}

构造函数(所有三个方法都在同一个类中):

    public Piece(int shape[][]) {
    super(shape);
    currentX = 7;
    currentY = 2;
    updateLocation();
}

此方法在另一个类中,它包含我要修改其属性的实例对象:

public void keyPressed(KeyEvent event) {
    int key = event.getKeyCode();
    switch (key) {
    case KeyEvent.VK_UP: // up arrow
    case KeyEvent.VK_KP_UP:
        currentPiece.rotateCounterclockwise();
        break;
    case KeyEvent.VK_DOWN: // down arrow
    case KeyEvent.VK_KP_DOWN:
        currentPiece.rotateClockwise();
        break;
    case KeyEvent.VK_LEFT: // left arrow
    case KeyEvent.VK_KP_LEFT:
        currentPiece.moveLeft();
        break;
    case KeyEvent.VK_RIGHT: // right arrow
    case KeyEvent.VK_KP_RIGHT:
        currentPiece.moveRight();
        break;
    case KeyEvent.VK_SPACE: //  space bar
        currentPiece.drop();
    }
}

createPiece 方法(我想访问 shape 属性):

public static Piece createPiece() {
    int[][] s = SHAPES[(int) (Math.random() * SHAPES.length)];
    switch ((int) (Math.random() * 10)) {
    case 0:
    case 1:
    case 2:
    case 3:
    default:
        return new Piece(s);
    }
}

整个代码可以在这里找到(没有某些修改):

http://mathcs.slu.edu/~fritts/cse131/labs/lab9/index.html

更新:

我发现 super 在 Grid 中调用了这个构造函数:

public Grid(int[][] contents) {
    this.contents = contents;
    Dimension d = new Dimension(getColumns()*Tetris.SQUARE_SIZE,
                                getRows()*Tetris.SQUARE_SIZE);
    setSize(d);
    setPreferredSize(d);
    setOpaque(false);
}

现在,我尝试了这个:

synchronized void rotateClockwise() {
Grid.contents = multiplyMatrix(Grid.contents);
updateLocation();

}

它给了我:

不能从静态上下文引用非静态方法 getContents()

4

1 回答 1

1

您没有向我们展示超类的代码,但一种方法可能是shape在子类中创建字段的镜像,覆盖 getter 并添加一个 setter;例如

  public class Super {
      private Shape shape;
      public Super(Shape shape) { this.shape = shape; }
      public Shape getShape() { return this.shape; }
  }

  public class Sub extends Super {
      private Shape myShape;

      public Sub(Shape shape) {
          super(shape);
          this.myShape = shape;
      }

      @Override
      public Shape getShape() { return this.myShape; }

      public void setShape(Shape shape) { this.myShape = shape; }
  }

这种方法只有在Super设计允许时才有效。特别是Super必须使用getShape()取值Shape

(如果不是为了支持扩展而设计的,有一些可怕Super的解决方法,但我敢肯定你的老师不希望你在这里使用它们......)

于 2013-02-14T22:51:21.983 回答