类继承-正确调用方法

时间:2019-07-20 10:57:40

标签: 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));

问题是,当isValid class的方法CheckRows运行时,它调用isValid的方法AbstractSolver,后者运行其方法{{1 },并调用超类的所有未实现的“抽象”方法,而不是调用子类的重写方法。这本可以在isValid中起作用。有没有办法在Java中修复它?更重要的是:是否有更好的最佳实践?

1 个答案:

答案 0 :(得分:2)

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

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

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

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


此后还有其他问题(numSet而不是_numSet,索引超出范围),但这是(大概)使您认为抽象方法而不是重写方法的问题被呼叫。