data
类中类型为 Array
但没有被重写的 equals()
或 hashCode()
的属性。
数组形参通过引用相等进行了比较,这可能是一种意外行为。
在此类情况下,强烈建议重写 equals()
和 hashCode()
。
示例:
data class Text(val lines: Array<String>)
该快速修复会生成缺失的 equals()
和 hashCode()
实现:
data class Text(val lines: Array<String>) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as Text
if (!lines.contentEquals(other.lines)) return false
return true
}
override fun hashCode(): Int {
return lines.contentHashCode()
}
}