大家好,我試圖用==
檢查兩個變量在結構上等於數據類平等科特林
// 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