我试图为我班级重载compareTo
和equals
操作员.
比较运算符没有问题.它既可以作为成员也可以作为扩展功能.
equals运算符必须是成员,覆盖等于fun.
class MyClass {
companion object {
private val NUMBER: Int = 5
operator fun compareTo(value: Int) = NUMBER - value
override operator fun equals(other: Any?) =
when (other) {
is Int -> NUMBER == other
else -> throw Exception("")
}
}
}
fun test() {
if (MyClass < 10) {
//ok
}
//Operator '==' cannot be applied to 'MyClass.companion' and kotlin.Int
if (MyClass == 5) {
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:如何正确地重载'=='?
我正在使用android SIP for VoIP.应用程序正在成功接收呼叫.但是,启动呼叫会产生一些错误.
日志中没有错误,但信息说:
" I/art: Thread[1,tid=23775,WaitingForJniOnLoad,Thread*=0xb4f07800,peer=0x759512e0,"main"] recursive attempt to load library "/system/lib/librtp_jni.so" "
任何人都可以解释什么是问题,我们怎么可能解决它?