如何选择在Kotlin派生类中重写哪个Java重载?
我试图在Kotlin中实现扩展java.util.AbstractList
的MyIntList
。 AbstractList
定义了E remove(int index)
和boolean remove(Object o)
。 我使用下面的代码:
class IntList() : AbstractList() { .... override fun remove(index: Int): Int { .... } }
但是这给出了一个error: return type of 'remove' is not a subtype of the return type of the overridden member 'public open fun remove(element: kotlin.Int!): kotlin.Boolean defined in java.util.AbstractList' override fun remove(index: Int): Int {
我如何告诉Kotlin我试图重写E remove(int index)
? (并从哪里得到remove(Int):Boolean
重载?)
结果是最后一句话是线索。 有一些编译器shenanigans,它认为我正在实现MutableList
。 而MutableList
定义了一个removeAt(element: E): Boolean
,它静静地编译成一个remove
方法,改变名称。 当我重写removeAt
一切正常。