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

标签 javascript abstract-class es6-class

我曾长期使用 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/

相关文章:

javascript - JavaScript 中的 `new` 只是 `.call` 的语法糖吗?

javascript - 使用 jquery/javascript 从 div 中加载的外部网页获取 url

javascript - 在 ES6 React 类的函数中返回变量或常量

es6-class - react 路由器 : how to pass "match" object into a component declared as an ES6 class?

javascript - 何时使用 ES6 类以及何时在 javascript 中使用函数

javascript - IE 中的页面加载时,动画 gif 不会显示动画

javascript - 有没有办法将html页面的数据导出为PDF?

inheritance - 从抽象类继承的正确 UML 建模

c# - 抽象类中的公共(public)事件

C++ 抽象类和继承