0

我想从我的子类中setValueAt(int row, int col, int value)的超类扩展方法。NumberBoardSudoku

在数独中,value值为空或 1 到 9,但在超类NumberBoard中,值可能为空或>= 0. 如何在我的子类中更改它Sudoku

超类NumberBoard(我不能改变超类):

public class NumberBoard {

/** Value of an empty cell. */
public static final int EMPTY = -1;

/** The board's state. */
private int[][] board;

/**
 * Sets the value of the given cell.
 * 
 * @param row
 *            the cell's row, starting at 0.
 * @param col
 *            the cell's column, starting at 0.
 * @param value
 *            the cell's value. Must be {@code >= 0} or {@link #EMPTY}.
 */
public void setValueAt(int row, int col, int value) {
    if (isInRange(row, col) && (value == EMPTY || value >= 0)) {
        board[row][col] = value;
    }
}


/**
 * Checks if the given coordinates identify a valid cell on the board.
 * 
 * @param row
 *            the cell's row, starting at 0.
 * @param col
 *            the cell's column, starting at 0.
 * @return {@code true} if the coordinate is in range, {@code false}
 *          otherwise.
 */
protected final boolean isInRange(int row, int col) {
    return 0 <= row
            && row < board.length
            && 0 <= col
            && col < board[row].length;
  }
}

还有我的子类代码Sudoku(不幸的是没有一点):

public class Sudoku extends NumberBoard {


public void setValueAt(int row, int col, int value) {
    super.setValueAt(row, col, value);      
    }
  }
4

4 回答 4

1

检查子类中的值,例如

public class Sudoku extends NumberBoard {

    public void setValueAt(int row, int col, int value) {
        if(value <= 9 && value >= 1 ) {
            super.setValueAt(row, col, value);      
        } else {
           throw IllegalArgumentException ("value can be empty or between 1 & 9 (inclusive)")
        }

    }
}

在这里您可以接受 null,因为 int 不允许 null 值。

于 2016-12-15T18:54:01.717 回答
1

我认为您正在在这里寻找覆盖,以重新定义“价值”可以采用的值范围。

您可以进行覆盖,如下所示,

public class Sudoku extends NumberBoard {

  @Override
  public void setValueAt(int row, int col, int value) {
    if (value > 0 && value <= 9) { // Considering EMPTY just means 0 as int is primitive
      super.setValueAt(row, col, value);
    } else {
      throw new IllegalArgumentException("Value cannot be negative or greater than 9");
    }
  }
}

但是,更好的设计是拥有一个单独的 EnhancedNumberBoard 类(如果需要,则为本地内部类)覆盖该方法并将其用作对象组合的成员变量。

于 2016-12-15T19:17:55.807 回答
1

在 Sudoku 中,该值为空或 1 到 9,但在超类 NumberBoard 中,该值可能为空或 >= 0。如何在我的子类 Sudoku 中更改它?

您可以创建自己的异常来处理错误情况:

public class IncorectValueException extends Exception{
  public IncorectValueException(){
    super();
  }
}

此外,在NumberBoard类中,setValueAt()如果要根据类重新定义检查有效数据的行为,则在方法中执行过多的单一处理,应将其分组在特定方法中:

`isInRange(row, col) && (value == EMPTY || value >= 0)`

可能是一种新方法:isAcceptableValue(int row, int col, int value)

更改 :

public void setValueAt(int row, int col, int value) throws IncorectValueException{
    if (isInRange(row, col) && (value == EMPTY || value >= 0)) {
        board[row][col] = value;
    }
}

至 :

public void setValueAt(int row, int col, int value) throws IncorectValueException{
    if (!isAcceptableValue(row, col)) {
       throw new IncorectValueException();             
    }
      board[row][col] = value;
}

public boolean isAcceptableValue(int row, int col, int value){
   if(isInRange(row, col) && (value == EMPTY || value >= 0)){
     return true;
   }
   return false;
}

现在, Sudoku课程可能是这样的:

public class Sudoku extends NumberBoard {
     ...
public boolean isAcceptableValue(int row, int col, int value){ 
   if(isInRange(row, col) && (value==EMPTY || (value >= 1 && value <= 9))) {
      return true;             
    }       
    return false;
}
于 2016-12-15T19:08:07.180 回答
1

看了你的问题几次,我终于明白你想要什么了。如果不想运行继承自超类的实现,可以override使用方法:

public class Sudoku extends NumberBoard {

    @Override
    public void setValueAt(int row, int col, int value) {
        //do not invoke super.setValueAt() here
        //Write your new implementation here
    }
}

setValueAt()通过在其中编写一组新的实现来覆盖。

于 2016-12-15T18:59:09.280 回答