2016-08-17 47 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

使用抽象類,而不是一個特質 – Samar

回答

12

你可以使用一個抽象類

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

case class Pawn(player: Int, pos: Pos) extends Piece(player, pos) 

或(可能更好),您可以定義一個抽象特質的成員

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

case class Pawn(player: Int, pos: Pos) extends Piece