当前位置: 首页 > 面试题库 >

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

桂志诚
2023-03-14
问题内容

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

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

错误是:Implicit super constructor Board() is undefined. Must explicitly invoke another constructor

密集板

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[][]){
      super(t.length, t[0].length);
 }


 类似资料:
  • 问题内容: 我有一个带有私有构造函数的类’ClassA’。 现在,我正在扩展类’ClassA’[ 在执行此操作之前删除了 final 关键字] 现在,我得到了。这是什么意思,如何解决? 注意 我不能更改ClassA构造函数的访问说明符。 问题答案: 将ClassA的构造方法可见性从更改为。 构造函数总是从调用超类构造函数开始。如果构造函数显式包含对超类构造函数的调用,则使用该构造函数。否则,将隐含

  • 可能的重复: 为什么this()和super()必须是构造函数中的第一条语句? 为什么子类构造函数必须显式调用超类构造函数?这是什么原因呢?

  • 如果将移到构造函数的最后一行,我不明白为什么下面的代码会显示错误。 我已经检查了很多关于StackOverflow的答案,但我仍然不能理解这其中的原因。请帮我用一些简单的例子和解释弄清楚这个错误。

  • 问题内容: 这是面试中提出的问题。如果一个类在Java中何时有多个构造函数,我们可以从另一个构造函数调用另一个函数吗? 问题答案: 可以,我知道的语法是 您还可以通过以下方式调用超类的构造函数 这两个调用只能作为构造函数中的第一条语句完成(因此,您只能在执行其他任何操作之前调用另一个构造函数)。

  • 问题内容: 以下代码向我返回错误信息: 我不明白。我的代码中的构造函数是第一条语句。我究竟做错了什么? 问题答案: 构造函数名称必须与类名称相同,因此请将类名称更改为或将构造函数名称更改为。 示例 (请注意,在Java中通常第一个字母是大写字母) :

  • 我试图用Java做一些事情,但在做构造函数时遇到了问题。我的代码: 工件构造器 Bishop构造函数(从工件扩展): 当我尝试这样做时,我在超级调用中得到一个错误:“super()必须是构造函数体中的第一个语句”。 我看到的唯一修复方法是将super()调用中的“symbole”替换为一个表达式,该表达式将在函数“color”中获得正确的符号,但我无法做到这一点? 我的Symbol类只是一个枚举,