Last active
April 30, 2021 21:12
-
-
Save patbeagan1/77220cb1e99e1c03d65dc48e1bbd5343 to your computer and use it in GitHub Desktop.
Hashcode generation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* You can edit, run, and share this code. | |
* play.kotlinlang.org | |
*/ | |
val test1 = "test1" | |
val test2 = "test2" | |
val test3 = "test3" | |
val test4 = null | |
open class A { | |
protected fun Int.lhash(i: Any?) = 31 * this + i.hashCode() | |
open fun hashcode(): Int = 31 | |
.lhash(test1) | |
.lhash(test4) | |
.lhash(test2) | |
.lhash(test3) | |
} | |
class B: A(){ | |
val a = 5 | |
override fun hashcode():Int { | |
return super.hashcode().lhash(a) | |
} | |
} | |
fun main() { | |
println("hashCode4=>${A().hashcode()}") | |
println("hashCode4=>${B().hashcode()}") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment