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