创建一个名为 RubikRight 的魔方类的子类



我有一个魔方类,内容如下:

public class Rubik{
private int[][][] grid;
protected Face[] cube;
protected Face up;
protected Face left;
protected Face front;
protected Face right;
protected Face down;
protected Face back;
private static String dots = "......";
private String output;
//Constructor
public Rubik(int[][][] grid){
int[][][] copy_grid = new int[6][3][3];
for (int k = 0; k < 6; k++){
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++)
copy_grid[k][i][j] = grid[k][i][j];
}
}
this.grid = copy_grid;
this.up = new Face(grid[0]);
this.left = new Face(grid[1]);
this.front = new Face(grid[2]);
this.right = new Face(grid[3]);
this.down = new Face(grid[4]);
this.back = new Face(grid[5]);
this.cube = new Face[]{this.up, this.left, this.front, this.right, this.down, this.back};
}

我正在尝试创建一个扩展魔方的RubikRight类,并且RubikRight的方向使得原始魔方的右面现在朝前。这就是我如何定义我的魔方构造函数:

public class RubikRight extends Rubik{
//Constructor
public RubikRight(int[][][] grid){
int[][][] copy_grid = new int[6][3][3];
for (int k = 0; k < 6; k++){
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++)
copy_grid[k][i][j] = grid[k][i][j];
}
}
this.grid = copy_grid;
this.up = new Face(grid[0]);
this.left = new Face(grid[2]);
this.front = new Face(grid[3]);
this.right = new Face(grid[5]);
this.down = new Face(grid[4]);
this.back = new Face(grid[1]);
this.cube = new Face[]{this.up, this.left, this.front, this.right, this.down, this.back};
}

但是,我收到错误,说

Constructor Rubik in class Rubik cannot be applied to the given type;
public RubikRight(int[][][] grid){
^
required: int[][][]
found: no arguments
reason: actual and formal argument lists differ in length
1 error

我可以知道为什么我似乎不能这样定义魔方吗?

每当实例化子类对象时,都会隐式调用父类默认构造函数。因此,当您通过调用new RubikRight(int[][][])来实例化RubikRight时,它会从 RubikRight 的构造函数内部隐式调用super()。因此错误:

required: int[][][] // what you have in Rubik class, note that you don't have the default constructor
found: no arguments // super(/* no argument */) called implicitly

要删除错误,您有两个选择:

从构造
  1. 函数显式调用RubikRightsuper(grid)
  2. 或者,在Rubik(基(类中实现默认构造函数。

你没有调用父类的构造函数。

在这里你可以做的是在父类中重载构造函数。 创建默认构造函数(无参数构造函数(并使其受到保护。

public class Rubik{
...
protected Rubik() {
}
}

这应该可以解决您的问题。

相关内容

  • 没有找到相关文章

最新更新