Class 继承 - 正确调用方法
Class inheritance - properly calling methods
我在 Java
从事了长期的编程工作。
我正在解决一个编码问题并尝试编写一个抽象解决方案 class,它将扩展三个 classes:
var isValidSudoku = function(board) {
return new CheckRows(board).isValid()
// && checkCols(board)
// && checkBoxes(board);
};
class AbstractSolver {
constructor(board) {
this._board = board;
this._numSet = new Set();
this._state = {
x: 0,
y: 0,
}
}
getCell() {
const numString = this._board[this._state.y][this._state.x];
return isNumBetween0And9(numString) ?
{
isNum: true,
num: parseInt(numString, 10),
} :
{
isNum: false,
};
}
nextCell() {}
nextBlock() {}
isBlockFinish() {}
isBoardFinish() {}
isValid() {
while (this.isBoardFinish() == false) {
while (this.isBlockFinish() == false) {
const {
isNum,
num,
} = this.getCell();
if (isNum == false) {
// do nothing
} else if (this._numSet.has(num)) {
return false;
} else {
this._numSet.add(num);
}
this.nextCell();
}
this.numSet.clear();
this.nextBlock();
}
return true;
}
}
function check(a) {
return f => f(a);
}
function isNumBetween0And9(i) {
const checkNum = check(i);
return checkNum(Number.isInteger) && checkNum(x => x >= 0) && checkNum(x => x <= 9);
}
class CheckRows extends AbstractSolver {
constructor(board) {
super(board);
this._boardLen = 9;
}
nextCell() {
this._state = {
x: this._state.x + 1,
y: this._state.y,
};
}
nextBlock() {
this._state = {
x: 0,
y: this._state.y + 1,
};
}
isBlockFinish() {
this._state.x >= this._boardLen;
}
isBoardFinish() {
this._state.x >= this._boardLen && this.state.y >= this._boardLen;
}
}
const testParam = [
["5", "3", ".", ".", "7", ".", ".", ".", "."],
["6", ".", ".", "1", "9", "5", ".", ".", "."],
[".", "9", "8", ".", ".", ".", ".", "6", "."],
["8", ".", ".", ".", "6", ".", ".", ".", "3"],
["4", ".", ".", "8", ".", "3", ".", ".", "1"],
["7", ".", ".", ".", "2", ".", ".", ".", "6"],
[".", "6", ".", ".", ".", ".", "2", "8", "."],
[".", ".", ".", "4", "1", "9", ".", ".", "5"],
[".", ".", ".", ".", "8", ".", ".", "7", "9"]
];
const testParam2 = [
["5", "3", "3", ".", "7", ".", ".", ".", "."],
["6", ".", ".", "1", "9", "5", ".", ".", "."],
[".", "9", "8", ".", ".", ".", ".", "6", "."],
["8", ".", ".", ".", "6", ".", ".", ".", "3"],
["4", ".", ".", "8", ".", "3", ".", ".", "1"],
["7", ".", ".", ".", "2", ".", ".", ".", "6"],
[".", "6", ".", ".", ".", ".", "2", "8", "."],
[".", ".", ".", "4", "1", "9", ".", ".", "5"],
[".", ".", ".", ".", "8", ".", ".", "7", "9"]
];
console.log(isValidSudoku(testParam2));
问题是,当 class
CheckRows
的方法 isValid
运行时,它调用 AbstractSolver
的方法 isValid
运行它的方法 isValid
并调用超 class 的所有未实现的 "abstract" 方法,而不是调用子 class 的重写方法。这在 Java
中有效。有没有办法在 JS
中修复它?更重要的是:是否有更好的最佳实践?
问题不在于调用了错误的方法(调用了正确的方法),问题在于这些方法没有 return 值。就像在 Java 中一样,您需要 return
关键字来 return 来自方法的值:
isBlockFinish() {
return this._state.x >= this._boardLen;
//^^^^^^
}
isBoardFinish() {
return this._state.x >= this._boardLen && this.state.y >= this._boardLen;
//^^^^^^
}
如果您使用简洁的函数体(例如,() => value
),箭头函数有一个隐含的 return,但方法没有。
之后还有其他问题(numSet
而不是 _numSet
,索引超出范围),但正是这个问题(大概)让你想到了抽象方法而不是正在调用被覆盖的。
我在 Java
从事了长期的编程工作。
我正在解决一个编码问题并尝试编写一个抽象解决方案 class,它将扩展三个 classes:
var isValidSudoku = function(board) {
return new CheckRows(board).isValid()
// && checkCols(board)
// && checkBoxes(board);
};
class AbstractSolver {
constructor(board) {
this._board = board;
this._numSet = new Set();
this._state = {
x: 0,
y: 0,
}
}
getCell() {
const numString = this._board[this._state.y][this._state.x];
return isNumBetween0And9(numString) ?
{
isNum: true,
num: parseInt(numString, 10),
} :
{
isNum: false,
};
}
nextCell() {}
nextBlock() {}
isBlockFinish() {}
isBoardFinish() {}
isValid() {
while (this.isBoardFinish() == false) {
while (this.isBlockFinish() == false) {
const {
isNum,
num,
} = this.getCell();
if (isNum == false) {
// do nothing
} else if (this._numSet.has(num)) {
return false;
} else {
this._numSet.add(num);
}
this.nextCell();
}
this.numSet.clear();
this.nextBlock();
}
return true;
}
}
function check(a) {
return f => f(a);
}
function isNumBetween0And9(i) {
const checkNum = check(i);
return checkNum(Number.isInteger) && checkNum(x => x >= 0) && checkNum(x => x <= 9);
}
class CheckRows extends AbstractSolver {
constructor(board) {
super(board);
this._boardLen = 9;
}
nextCell() {
this._state = {
x: this._state.x + 1,
y: this._state.y,
};
}
nextBlock() {
this._state = {
x: 0,
y: this._state.y + 1,
};
}
isBlockFinish() {
this._state.x >= this._boardLen;
}
isBoardFinish() {
this._state.x >= this._boardLen && this.state.y >= this._boardLen;
}
}
const testParam = [
["5", "3", ".", ".", "7", ".", ".", ".", "."],
["6", ".", ".", "1", "9", "5", ".", ".", "."],
[".", "9", "8", ".", ".", ".", ".", "6", "."],
["8", ".", ".", ".", "6", ".", ".", ".", "3"],
["4", ".", ".", "8", ".", "3", ".", ".", "1"],
["7", ".", ".", ".", "2", ".", ".", ".", "6"],
[".", "6", ".", ".", ".", ".", "2", "8", "."],
[".", ".", ".", "4", "1", "9", ".", ".", "5"],
[".", ".", ".", ".", "8", ".", ".", "7", "9"]
];
const testParam2 = [
["5", "3", "3", ".", "7", ".", ".", ".", "."],
["6", ".", ".", "1", "9", "5", ".", ".", "."],
[".", "9", "8", ".", ".", ".", ".", "6", "."],
["8", ".", ".", ".", "6", ".", ".", ".", "3"],
["4", ".", ".", "8", ".", "3", ".", ".", "1"],
["7", ".", ".", ".", "2", ".", ".", ".", "6"],
[".", "6", ".", ".", ".", ".", "2", "8", "."],
[".", ".", ".", "4", "1", "9", ".", ".", "5"],
[".", ".", ".", ".", "8", ".", ".", "7", "9"]
];
console.log(isValidSudoku(testParam2));
问题是,当 class
CheckRows
的方法 isValid
运行时,它调用 AbstractSolver
的方法 isValid
运行它的方法 isValid
并调用超 class 的所有未实现的 "abstract" 方法,而不是调用子 class 的重写方法。这在 Java
中有效。有没有办法在 JS
中修复它?更重要的是:是否有更好的最佳实践?
问题不在于调用了错误的方法(调用了正确的方法),问题在于这些方法没有 return 值。就像在 Java 中一样,您需要 return
关键字来 return 来自方法的值:
isBlockFinish() {
return this._state.x >= this._boardLen;
//^^^^^^
}
isBoardFinish() {
return this._state.x >= this._boardLen && this.state.y >= this._boardLen;
//^^^^^^
}
如果您使用简洁的函数体(例如,() => value
),箭头函数有一个隐含的 return,但方法没有。
之后还有其他问题(numSet
而不是 _numSet
,索引超出范围),但正是这个问题(大概)让你想到了抽象方法而不是正在调用被覆盖的。