美文网首页
同时使用了hashcode和clone,虽然直接打印对象返回的值

同时使用了hashcode和clone,虽然直接打印对象返回的值

作者: ElonYanJ | 来源:发表于2019-03-11 14:46 被阅读0次

    同时使用了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
    

    相关文章

      网友评论

          本文标题:同时使用了hashcode和clone,虽然直接打印对象返回的值

          本文链接:https://www.haomeiwen.com/subject/giqbpqtx.html