5
我試圖延長,在Java中,一個科特林委託類,並收到以下錯誤:我可以使用Java擴展Kotlin授權類嗎?
Cannot inherit from final 'Derived'
見下面的代碼。
我想要做的是裝飾一個類的方法。
任何想法爲什麼Kotlin將Derived
定義爲最終?有沒有辦法讓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