【发布时间】:2016-08-17 10:23:23
【问题描述】:
我想为国际象棋游戏建模。
为此,我想创建一个抽象类Piece,它将玩家和位置作为参数。从此,我想扩展到其他类,例如Pawn:
trait Piece(player: Int, pos: Pos) = {
def spaces(destination: Pos): List[Pos]
}
case class Pawn extends Piece = {
//some other code
}
但是,我认为我不允许将参数传递给特征,例如 trait Piece(player: Int, pos: Pos)。
那么我怎样才能拥有一个包含字段的抽象类Piece?
【问题讨论】:
-
使用抽象类而不是特征
-
Scala 3 支持 trait 中的参数。查看:dotty.epfl.ch/docs/reference/other-new-features/…了解更多详情
标签: scala