我试图将一些对象放在 mutableListOf 中,但所有这些对象最终都被覆盖了。
class Users {...}
class dbUsers{
var users = Users()
val list = mutableListOf(users)
fun addUser(phone: Int, name: String){
users.phone = phone
users.name = name
list.add(users)
}
}
fun main() {
var dbAccess = dbUsers()
dbAccess.addUser(8439, "Thiago")
dbAccess.addUser(12312, "Maria")
println(dbAccess.list[0].name)
println(dbAccess.list[1].name)
}
当我们在位置 1 打印时,我们看到这是覆盖