我正在开发“15 人游戏”的示例项目。游戏板上的单元格定义为:
package board
enum class Direction {
UP, DOWN, RIGHT, LEFT;
}
data class Cell(val i: Int, val j: Int) {
override fun toString()= "($i, $j)"
}
interface SquareBoard {
val width: Int
fun Cell.getNeighbour(direction: Direction): Cell?
}
interface GameBoard<T> : SquareBoard {
operator fun get(cell: Cell): T?
operator fun set(cell: Cell, value: T?)
fun find(predicate: (T?) -> Boolean): Cell?
}
我想从 GameOf15 类中的函数调用 Cell.getNeighbor 扩展函数
package games.gameOfFifteen
import board.Cell
import board.Direction
import board.GameBoard
import board.GameBoardImpl
import games.game.Game
class GameImpl(val initializer: GameOfFifteenInitializer) : Game
{
val board:GameBoard<Int?> = GameBoardImpl(4)
override fun processMove(direction: Direction) {
val openCell:Cell? = board.find { it == null}
//this line does not compile
val neighbor = openCell?.getNeighbor(direction)
}
}
上面的代码无法编译。我收到错误“未解析的参考:getNeighbor”。我不确定我在这里做错了什么。我还需要做些什么才能使扩展功能可见吗?