Must explicitly invoke another constructor

后端 未结 6 1052
借酒劲吻你
借酒劲吻你 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:42

    I think what you want to do is this

    public DenseBoard(Tile t[][]){
          this(t.length, t[0].length);
     }
    
    0 讨论(0)
  • 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.
    }
    
    0 讨论(0)
  • 2021-01-25 03:48

    You can just do this to send the values up to parent class.

    public DenseBoard(Tile t[][]){
          super(t.length, t[0].length);
     }
    
    0 讨论(0)
  • 2021-01-25 03:50

    I think you DenseBoard class should implement all the Board class's abstract method. such like this:

    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(){
    
      }
    }
    
    0 讨论(0)
  • 2021-01-25 03:59

    Your constructor needs to call the constructor of the superclass. If the superclass had defined a no-argument constructor, you would not be seeing this error. To expand on the other answers with some code, what you need is

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

    Or alternatively modify your Board class to include

    public Board()
    {
    ...
    }
    
    0 讨论(0)
  • 2021-01-25 04:06

    In your class Board you only have one constructor which requires 2 int arguments.

    And since DenseBoard extends Board, each of the constructor in DenseBoard should call some constructor in Board.

    If Board had a no-arg constructor, the compiler would automatically call it for you. But since you don't have one, you should call another constructor explicitly from your public DenseBoard(Tile t[][]).

    0 讨论(0)
提交回复
热议问题