为什么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
Class DenseBoard
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();
}
答案 0 :(得分:1)
我认为你想要做的就是这个
public DenseBoard(Tile t[][]){
this(t.length, t[0].length);
}
答案 1 :(得分:1)
只是指出
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.
}
答案 2 :(得分:0)
在您的班级Board
中,您只有一个构造函数需要2 int
个参数。
由于DenseBoard
扩展Board
,DenseBoard
中的每个构造函数都应调用Board
中的一些构造函数。
如果Board
有一个no-arg构造函数,编译器会自动为你调用它。但是由于你没有,你应该从你的public DenseBoard(Tile t[][])
明确地调用另一个构造函数。
答案 3 :(得分:0)
您的构造函数需要调用超类的构造函数。如果超类定义了无参数构造函数,则不会看到此错误。要使用某些代码扩展其他答案,您需要的是
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?
}
或者修改您的Board
课程以包含
public Board()
{
...
}
答案 4 :(得分:0)
您可以这样做将值发送到父类。
public DenseBoard(Tile t[][]){
super(t.length, t[0].length);
}
答案 5 :(得分:0)
我认为你的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(){
}
}