简体   繁体   English

创建一个名为RubikRight的Rubik类的子类

[英]Creating a child class of Rubik class called RubikRight

I have a Rubik class with the following: 我有一个Rubik课,内容如下:

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};
    }

And I am trying to create a RubikRight class which extends the Rubik, and RubikRight is orientated in such a way that the right face of the original Rubik is now facing front. 我正在尝试创建一个扩展Rubik的RubikRight类,并且RubikRight的定向方式使得原始Rubik的右面现在面向前方。 This is how I am defining my constructor for 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};
    }

However, I am getting the error which says 但是,我得到的错误是

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

May I know why is it that I can't seem to define RubikRight that way? 我可以知道为什么我似乎不能那样定义RubikRight吗?

Whenever you a instantiate a child class object the parent class default constructor is called implicitly. 每当您实例化一个子类对象时,父类的默认构造函数都会被隐式调用。 So, when you instantiated RubikRight by calling new RubikRight(int[][][]) it implicitly called the super() from inside the RubikRight's constructor. 因此,当您通过调用new RubikRight(int[][][])实例化RubikRight ,它从RubikRight的构造函数内部隐式调用了super() And hence the error: 因此错误:

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

To remove the error, you have two options: 要消除该错误,您有两个选择:

  1. Either call super(grid) from RubikRight constructor explicitly. RubikRight构造函数中显式调用super(grid)
  2. Or, implement the default constructor in the Rubik (base) class. 或者,在Rubik (基)类中实现默认的构造函数。

You are not calling constructor of parent class. 您没有在调用父类的构造函数。

What you can do here is, overload constructor in parent class. 您可以在此处执行的是父类中的重载构造函数。 Create default constructor (no parameter constructor) and make it protected. 创建默认构造函数(无参数构造函数)并使其受到保护。

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

This should solve your problem. 这应该可以解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM