我有以下类(class)作为其他一些类(class)的基础:
abstract class BaseFitness: Comparable<BaseFitness> {
var valid: Boolean = false
fun invalidate() { valid = false }
abstract fun value(): Double
abstract fun copy(): BaseFitness
override operator fun equals(other: Any?): Boolean =
if (other is BaseFitness)
this.value() == other.value()
else
throw IllegalArgumentException("Can only compare to another class derived from BaseFitness.")
override fun compareTo(other: BaseFitness): Int =
this.value().minus(other.value())
.let { if (it.isNaN()) 0.0 else it }
.sign.toInt()
@JvmName("compareToOperator")
operator fun compareTo(other: Any?): Int =
if (other is BaseFitness)
this.compareTo(other)
else
throw IllegalArgumentException("Can only compare to another class derived from BaseFitness.")
}
现在,在本课中,我想实现基本的比较行为。
基本上,我希望能够做到:
if (fit1 == fit2) { /* do stuff */ }
if (fit1 > fit2) { /* do stuff */ }
if (fit1 < fit2) { /* do stuff */ }
myFitnessSequence.max()
equals()
和compareTo()
运算符函数负责前3个项目。但是对于最后一项,我需要BaseFitness
来实现Comparable
。问题是,当我实现可比性并添加非运算符compareTo()
方法时,equals()
会因以下消息而中断:'operator' modifier is inapplicable on this function: must override ''equals()'' in Any
我尝试做与
compareTo()
相同的操作,只是有2种实现equals()
,一种标记为运算符,一种保留为方法,然后将@JvmName
添加到其中一种,我得到了'@JvmName' annotation is not applicable to this declaration
现在,我的选择基本上是选择保留和
==
运算符,但不实现Comparable
或放弃==
以使用可比性。
最佳答案
您无需使用equals
关键字标记compareTo
和operator
,这两个都已在各自的类型中定义为运算符。
对于equals
,Any
将其定义为operator
,并且compareTo
在operator
Comparable
中标记为interface
。
因此,您要做的就是通过覆盖它们来提供它们的实现。
从Kotlin in Action
Regarding the equals method
The equals function is marked as override, because, unlike other conventions, the method implementing it is defined in the Any class (equality comparison is supported for all objects in Kotlin). That also explains why you don’t need to mark it as operator: the base method in Any is marked as such, and the operator modifier on a method applies also to all methods that implement or override it.
Regarding the Comparable interface
Kotlin supports the same Comparable interface. But the compareTo method defined in that interface can be called by convention, and uses of comparison operators ('<', '>', <=, and >=) are translated into calls of compareTo.
关于inheritance - Kotlin界面实现打破了 `equals`覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60489771/