我有一個一流的第三方Java庫一樣科特林接口的Java類:意外覆蓋
public class ThirdParty {
public String getX() {
return null;
}
}
我也有在科特林像
interface XProvider {
val x: String?
}
接口現在我想延長ThirdParty
類並實現XProvider
接口。這已經在我的遺留Java代碼做工精細:
public class JavaChild extends ThirdParty implements XProvider {}
不過,我想寫出儘可能多的科特林越好,我試圖將我的java類科特林。可悲的是,以下不工作:
class KotlinChild: ThirdParty(), XProvider
錯誤是
class 'KotlinChild1' must be declared abstract or implement abstract member public abstract val x: String? defined in XProvider
但是,如果我這樣做
class KotlinChild1: ThirdParty(), XProvider {
override val x: String? = null
}
我得到
error: accidental override: The following declarations have the same JVM signature (getX()Ljava/lang/String;)
fun <get-x>(): String?
fun getX(): String!
override val x: String? = null
什麼工作是以下醜陋的變通:
class KotlinChild: JavaChild()
你什麼錯誤? – marstran
對不起,忘了。我更新了問題 – dpoetzsch
[解決Kotlin中的意外覆蓋錯誤]的可能的重複(http://stackoverflow.com/questions/32970923/resolving-accidental-override-errors-in-kotlin) – mfulton26