2016-08-17 10 views
2

チェスのゲームをモデルにしたい。 そのために、抽象クラス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をどうすればできますか?

+0

をメンバーを定義します(おそらくより良い)

abstract class Piece(player: Int, pos: Pos) { ... } case class Pawn(player: Int, pos: Pos) extends Piece(player, pos) 

または抽象クラスを使用することができます – Samar

答えて

12

あなたが代わり形質の抽象クラスを使用した形質で抽象的に

trait Piece { 
    def player: Int 
    def pos: Pos 
    ... 
} 

case class Pawn(player: Int, pos: Pos) extends Piece 
関連する問題