Java 必须显式调用另一个构造函数

Java 必须显式调用另一个构造函数,java,Java,为什么Eclipse总是在构造函数上给我错误: public DenseBoard(Tile t[][]){ Board myBoard = new DenseBoard(t.length, t[0].length); } 错误是:Implicit super constructor Board()未定义。必须显式调用另一个构造函数 班级登黑板 package Game2048; // Tracks the positions of an arbitrary 2D grid

为什么Eclipse总是在构造函数上给我错误:

 public DenseBoard(Tile t[][]){
      Board myBoard = new DenseBoard(t.length, t[0].length);
  }
错误是:
Implicit super constructor Board()未定义。必须显式调用另一个构造函数

班级登黑板

package Game2048;

// Tracks the positions of an arbitrary 2D grid of Tiles.  DenseBoard
// uses an internal, multi-dimensional array to store the tiles and
// thus has a O(R * C) memory footprint (rows by columns).
public class DenseBoard extends Board {

  // Build a Board of the specified size that is empty of any tiles
  public DenseBoard(int rows, int cols){
      super(rows, cols);
  }

  // Build a board that copies the 2D array of tiles provided Tiles
  // are immutable so can be referenced without copying but the a
  // fresh copy of the 2D array must be created for internal use by
  // the Board.
  public DenseBoard(Tile t[][]){
      Board myBoard = new DenseBoard(t.length, t[0].length);
  }
package Game2048;

public abstract class Board{

  protected int rows;
  protected int cols;


  public Board(int rows, int cols){
      this.rows = rows;
      this.cols = cols;
  }
  // Create a distinct copy of the board including its internal tile
  // positions and any other state
  public abstract Board copy();

  // Return the number of rows in the Board
  public abstract int getRows();

  // Return the number of columns in the Board
  public abstract int getCols();

  // Return how many tiles are present in the board (non-empty spaces)
  public abstract int getTileCount();

  // Return how many free spaces are in the board
  public abstract int getFreeSpaceCount();

  // Get the tile at a particular location
  public abstract Tile tileAt(int i, int j);

  // true if the last shift operation moved any tile; false otherwise
  public abstract boolean lastShiftMovedTiles();

  // Return true if a shift left, right, up, or down would merge any
  // tiles. If no shift would cause any tiles to merge, return false.
  // The inability to merge anything is part of determining if the
  // game is over.
  public abstract boolean mergePossible();

  // Add a the given tile to the board at the "freeI"th free space.
  public abstract void addTileAtFreeSpace(int freeI, Tile tile);

  // Shift the tiles of Board in various directions.  Any tiles that
  // collide and should be merged should be changed internally in the
  // board.  Shifts only remove tiles, never add anything.  The shift
  // methods also set the state of the board internally so that a
  // subsequent call to lastShiftMovedTiles() will return true if any
  // Tile moved and false otherwise.  The methods return the score
  // that is generated from the shift which is the sum of the scores
  // all tiles merged during the shift. If no tiles are merged, the
  // return score is 0.
  public abstract int shiftLeft();
  public abstract int shiftRight();
  public abstract int shiftUp();
  public abstract int shiftDown();

}
班级董事会

package Game2048;

// Tracks the positions of an arbitrary 2D grid of Tiles.  DenseBoard
// uses an internal, multi-dimensional array to store the tiles and
// thus has a O(R * C) memory footprint (rows by columns).
public class DenseBoard extends Board {

  // Build a Board of the specified size that is empty of any tiles
  public DenseBoard(int rows, int cols){
      super(rows, cols);
  }

  // Build a board that copies the 2D array of tiles provided Tiles
  // are immutable so can be referenced without copying but the a
  // fresh copy of the 2D array must be created for internal use by
  // the Board.
  public DenseBoard(Tile t[][]){
      Board myBoard = new DenseBoard(t.length, t[0].length);
  }
package Game2048;

public abstract class Board{

  protected int rows;
  protected int cols;


  public Board(int rows, int cols){
      this.rows = rows;
      this.cols = cols;
  }
  // Create a distinct copy of the board including its internal tile
  // positions and any other state
  public abstract Board copy();

  // Return the number of rows in the Board
  public abstract int getRows();

  // Return the number of columns in the Board
  public abstract int getCols();

  // Return how many tiles are present in the board (non-empty spaces)
  public abstract int getTileCount();

  // Return how many free spaces are in the board
  public abstract int getFreeSpaceCount();

  // Get the tile at a particular location
  public abstract Tile tileAt(int i, int j);

  // true if the last shift operation moved any tile; false otherwise
  public abstract boolean lastShiftMovedTiles();

  // Return true if a shift left, right, up, or down would merge any
  // tiles. If no shift would cause any tiles to merge, return false.
  // The inability to merge anything is part of determining if the
  // game is over.
  public abstract boolean mergePossible();

  // Add a the given tile to the board at the "freeI"th free space.
  public abstract void addTileAtFreeSpace(int freeI, Tile tile);

  // Shift the tiles of Board in various directions.  Any tiles that
  // collide and should be merged should be changed internally in the
  // board.  Shifts only remove tiles, never add anything.  The shift
  // methods also set the state of the board internally so that a
  // subsequent call to lastShiftMovedTiles() will return true if any
  // Tile moved and false otherwise.  The methods return the score
  // that is generated from the shift which is the sum of the scores
  // all tiles merged during the shift. If no tiles are merged, the
  // return score is 0.
  public abstract int shiftLeft();
  public abstract int shiftRight();
  public abstract int shiftUp();
  public abstract int shiftDown();

}

我想你想做的就是这样

public DenseBoard(Tile t[][]){
      this(t.length, t[0].length);
 }

在类
Board
中,只有一个构造函数需要2个
int
参数

由于
DenseBoard
扩展了
Board
,因此
DenseBoard
中的每个构造函数都应该调用
Board
中的某个构造函数


如果
Board
有一个无参数构造函数,编译器会自动为您调用它。但是由于您没有构造函数,因此应该从
公共DenseBoard(Tile t[][])显式调用另一个构造函数。
您的构造函数需要调用超类的构造函数。如果超类定义了一个无参数构造函数,您就不会看到这个错误。要用一些代码扩展其他答案,您需要的是

 public DenseBoard(Tile t[][]){
      //You need a way to figure meaningful values to the superconstructor here
      super(intVar1, intVar2);
      Board myBoard = new DenseBoard(t.length, t[0].length); //I think 
     //this now becomes unnecessary?
  }
或者修改
类以包括

public Board()
{
...
}

您可以这样做,将值发送到父类

public DenseBoard(Tile t[][]){
      super(t.length, t[0].length);
 }
我只是想指出

public DenseBoard(Tile t[][]) {
    Board myBoard = new DenseBoard(t.length, t[0].length);
}
myBoard
是局部变量,当您使用
new DenseBoard(Tile t[][])创建新对象时,将无法引用它


你可以用两种方法来做

public DenseBoard(Tile t[][]) {
    super(t.length, t[0].length); // calling super class constructor
}

// or
// I would prefer this
public DenseBoard(Tile t[][]) {
    this(t.length, t[0].length); // calling DenseBoard(int rows, int cols) constuctor, which is internally passing the value to super class.
}

我认为DenseBoard类应该实现Board类的所有抽象方法。 例如:

package Game2048;
public class DenseBoard extends Board {

  public DenseBoard(int rows, int cols){
      super(rows, cols);
  }

  public DenseBoard(Tile t[][]){
      Board myBoard = new DenseBoard(t.length, t[0].length);
  }

  public Board copy(){

  }

  public int getRows(){

  }

  public int getCols(){

  }

  public int getTileCount(){

  }

  public int getFreeSpaceCount(){

  }

  public Tile tileAt(int i, int j){

  }

  public boolean lastShiftMovedTiles(){

  }

  public boolean mergePossible(){

  }

  public void addTileAtFreeSpace(int freeI, Tile tile){

  }

  public int shiftLeft(){

  }

  public int shiftRight(){

  }

  public int shiftUp(){

  }

  public int shiftDown(){

  }
}

为什么要这样做
Board myBoard=newdenseboard(t.length,t[0].length)
@Shahzeb,那我该怎么办?
super(t.length,t[0].length)为什么
这个
,@IvoBeckers?
引用什么?
调用DenseBoard的另一个构造函数。或者,您可以执行
super(t.length,t[0].length)因为您在另一个构造函数中没有执行任何特殊操作