我知道在 Kotlin 类中会自动创建一个 equals 和 hashcode,如下所示:
data class CSVColumn(private val index: Int, val value: String) {
}
我的问题是,有没有办法让实现只使用这些属性之一(例如index),而无需自己编写代码。原本非常简洁的类现在看起来像这样:
data class CSVColumn(private val index: Int, val value: String) {
    override fun equals(other: Any?): Boolean {
        if (this === other) {
            return true
        }
        if (javaClass != other?.javaClass) {
            return false
        }
        other as CSVColumn
        if (index != other.index) {
            return false
        }
        return true
    }
    override fun hashCode(): Int {
        return index
    }
}
在带有 Lombok 的 Java 中,我可以执行以下操作:
@Value
@EqualsAndHasCode(of="index")
public class CsvColumn {
    private final int index;
    private final String value;
}
如果有办法告诉 Kotlin 类似的东西会很酷。
| 归档时间: | 
 | 
| 查看次数: | 12960 次 | 
| 最近记录: |