大家好,我试图用==
检查两个变量在结构上等于数据类平等科特林
// PersonImpl0 has the name variable in the primary constructor
data class PersonImpl0(val id: Long, var name: String="") {
}
// PersonImpl1 has the name variable in the body
data class PersonImpl1(val id: Long) {
var name: String=""
}
fun main(args: Array<String>) {
val person0 = PersonImpl0(0)
person0.name = "Charles"
val person1 = PersonImpl0(0)
person1.name = "Eugene"
val person2 = PersonImpl1(0)
person0.name = "Charles"
val person3 = PersonImpl1(0)
person1.name = "Eugene"
println(person0 == person1) // Are not equal ??
println(person2 == person3) // Are equal ??
}
在这里我得到了
false
true
为什么是它的2的输出第一种情况下变量不相等,第二种情况下变量相等?
感谢您清除此为我
你知道,你从来没有设置'person2.name'或'person3.name'到什么关系吗? – tynn