[英]Can I extend, in Java, a Kotlin delegating class?
我試圖在Java中擴展一個Kotlin委托類並得到以下錯誤:
無法繼承最終'衍生'
見下面的代碼。
我要做的是裝飾一個類的方法。
知道為什么Kotlin定義Derived
為final? 有沒有一種方法讓Derived
不是最終的,所以我可以繼承它?
Java的:
new Derived(new BaseImpl(10)) { // Getting the error on this line: `Cannot inherit from final 'Derived'`
};
科特林:
interface Base {
fun print()
}
class BaseImpl(val x: Int) : Base {
override fun print() { print(x) }
}
class Derived(b: Base) : Base by b
*此處的示例: https : //kotlinlang.org/docs/reference/delegation.html
Kotlin類默認為最終版 。 將該類標記為open
以擴展它(無論是來自Kotlin還是來自Java):
open class Derived(b: Base) : Base by b
事實上,這是一個委托類應該對Java互操作沒有影響(雖然我還沒有嘗試過)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.