編譯這個介面宣告我有錯誤
Kotlin: Platform declaration clash: The following declarations have the same JVM signature (Function(Lkotlin/jvm/functions/Function1;)V): fun Function(action: (Int) -> Unit): Unit defined in IInterface fun Function(action: (String) -> Unit): Unit defined in IInterface
interface IInterface {
fun Function(action: (Int) -> Unit)
fun Function(action: (String) -> Unit)
}
但是當我應用@JvmName屬性時出現錯誤
Kotlin: '@JvmName' annotation is not applicable to this declaration
interface IInterface {
@JvmName("Function1001")
fun Function(action: (Int) -> Unit)
fun Function(action: (String) -> Unit)
}
我試過將介面更改為抽象類,但沒有幫助。看起來@JvmName只能用于類宣告。
解決這個問題的最佳實踐是什么?
uj5u.com熱心網友回復:
默認情況下,@JvmName在介面和開放類中是不允許的。原因在對KT-20068的評論中有解釋:
@JvmName 對于可覆寫的簽名是有問題的,因為在 Kotlin 和 Java 中覆寫可能會開始不一致:
interface A { @get:JvmName("x") val foo: Foo } interface B { @get:JvmName("y") val foo: Foo } class C: A, B { // ??? override val foo = ... }
您可以通過添加來解決此限制
@Suppress("INAPPLICABLE_JVM_NAME")
但要注意危險。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/383400.html
標籤:科特林
