我有一個類有幾個參數,如class Building(val a: Int, val b: Int, val c: Int)
。我必須更新它的代碼是這樣的:更短的方式來獲取更新的不可變對象?
def updatedA(a: Int): Building = new Building(a, this.b, this.c)
def updatedB(b: Int): Building = new Building(this.a, b, this.c)
有沒有更簡單的方法來獲取更新的對象,如下所示?
def updatedA(newA: Int): Building = new { val a = newA } extends this // doesn't compile/ type is AnyRef instead of Building
如何那第二個想法會比第一個想法更短或更直接? – Jesper