scala – 获取更新的不可变对象的更短方法?
我有一个带有几个参数的类,例如类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 解决方法
你看过copyWith()
construction mechanism吗?
或者查看copy mechanism的案例类别.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |