我想从我的子类中setValueAt(int row, int col, int value)
的超类扩展方法。NumberBoard
Sudoku
在数独中,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);
}
}