同时使用了hashcode和clone,虽然直接打印对象返回的值是一样的,但是堆地址是不一样的
class ConfigureRevenueAnalysisKey : Cloneable {
var applicationDate: String = ""
public override fun clone(): ConfigureRevenueAnalysisKey {
return super.clone() as ConfigureRevenueAnalysisKey
}
override fun hashCode(): Int {
var hashcode = applicationDate.hashCode()
return hashcode
}
override fun equals(other: Any?): Boolean {
val keyOther = (other as ConfigureRevenueAnalysisKey)
return keyOther.applicationDate == applicationDate
}
}
object test {
@JvmStatic
fun main(strings: Array<String>) {
val a = ConfigureRevenueAnalysisKey()
a.applicationDate = "aa"
val b = a.clone()
println(a)
println(b)
println(b.applicationDate)
println(a === b)
}
}
输出:
3104
com.nujnay.moment.server.ConfigureRevenueAnalysisKey@c20
3104
com.nujnay.moment.server.ConfigureRevenueAnalysisKey@c20
aa
false
网友评论