0

我正在尝试设计几个继承部分函数的类,但我似乎无法完全正确地获得语法。我的超类看起来像这样:

abstract class Controller {

    val react:PartialFunction[Event,Unit]

}

子类看起来像:

class BoardRendererController(val renderer:BoardRenderer, val board:Board) extends Controller {

    override val react {
        case PieceMovedEvent(piece, origin, destination) => println("Moving now")
    }
}

但这无法编译并出现此错误

[ERROR] /workspace/pacman/src/main/scala/net/ceilingfish/pacman/BoardRendererController.scala:14: error: '=' expected but '{' found.
[INFO]  override val react {
[INFO]                            ^
[ERROR] /workspace/pacman/src/main/scala/net/ceilingfish/pacman/BoardRendererController.scala:17: error: illegal start of simple expression
[INFO] }
[INFO] ^

我已经尝试了很多变体,有人知道正确的语法是什么吗?

4

1 回答 1

7

除了 abhin4v 的简洁建议之外,您仍然必须在定义中提供类型注释,因此我建议在您的基类中添加以下内容:

type PFEU = PartialFunction[Event, Unit]

然后您的子类将如下所示:

class BoardRendererController(val renderer:BoardRenderer, val board:Board)
extends Controller
{
    override val react: PFEU = {
        case PieceMovedEvent(piece, origin, destination) => println("Moving now")
    }
}
于 2010-07-07T19:58:41.973 回答