我建立了以下课程:
class Player(val name: String, val onField: Boolean, val draft: Int, val perc: Int, val height: Int, val timePlayed: Int) {
override def toString: String = name
}
我正在尝试做
def play(team: List[Player]): List[Player] =
team map (p => new Player(p.name, p.onField, p.draft, p.perc, p.height, p.timePlayed + 1))
这实际上是将字段“timePlayed”加一,并返回新的玩家“列表”。
有没有更方便的方法来做到这一点?也许:
def play(team: List[Player]): List[Player] =
team map (p => p.timeIncremented())
我的问题是如何以更方便的方式实现 timeIncremented() ?这样我就不必这样做了:
new Player(p.name, p.onField, p.draft, p.perc, p.height, p.timePlayed + 1)
谢谢!