Must explicitly invoke another constructor

后端 未结 6 1102
借酒劲吻你
借酒劲吻你 2021-01-25 03:27

Why is Eclipse keeps giving me error on the constructor:

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


        
6条回答
  •  灰色年华
    2021-01-25 03:45

    Just to point out

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

    myBoard is local variable, you will not be able to refer it when you create a new object using new DenseBoard(Tile t[][]).


    You can do it in 2 ways.

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

提交回复
热议问题