gpt4 book ai didi

javascript - 类继承——正确调用方法

转载 作者:行者123 更新时间:2023-12-01 00:49:10 24 4
gpt4 key购买 nike

我曾长期使用 Java 进行编程。我正在解决一个编码问题,并尝试编写一个抽象解决方案类,该类将由三个类扩展:

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运行时,它会调用class的方法isValid AbstractSolver 运行其方法 isValid 并调用父类(super class)的所有未实现的“抽象”方法,而不是调用子类的重写方法。这在 Java 中是可行的。有没有办法在 JS 中修复它?更重要的是:是否有更好的最佳实践?

最佳答案

问题不在于调用了错误的方法(正确的方法),问题在于这些方法不返回值。就像在 Java 中一样,您需要 return 关键字从方法返回值:

isBlockFinish() {
return this._state.x >= this._boardLen;
//^^^^^^
}

isBoardFinish() {
return this._state.x >= this._boardLen && this.state.y >= this._boardLen;
//^^^^^^
}

如果您使用简洁的函数体(例如,() => value),箭头函数就会有隐式返回,但方法不会。

<小时/>

此后还有更多问题(numSet 而不是 _numSet,索引超出范围),但这就是(大概)让您认为抽象的问题正在调用方法而不是重写的方法。

关于javascript - 类继承——正确调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57124080/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com