我有这样的data class:
data class BookObject(
val description: String?,
val owningCompanyData: OwningCompanyData?,
) {
var id: String? = null
var createdAt: Instant? = null
var createdBy: String? = null
var modifiedAt: Instant? = null
fun update(command: CreateOrUpdateBookObjectCommand): BookObject =
this.copy(
description = command.description,
owningCompanyData = command.owningCompanyData
)
}
Run Code Online (Sandbox Code Playgroud)
当我对update具有完全填充字段的对象使用该函数时,我得到一个具有空id, createdAt, createdBy,modifiedAt字段的对象(它们等于null)。但为什么会出现这种情况呢?为什么这些领域失去了它们的价值?
使用 copy() 函数复制对象,允许您更改其某些属性,同时保持其余属性不变。
| 归档时间: |
|
| 查看次数: |
136 次 |
| 最近记录: |