我可以使用Java扩展一个Kotlin授权类吗?
我想在Java中扩展一个Kotlin委托类,并得到以下错误:
不能从最终的“派生”
看下面的代码。
我想要做的是装饰一个类的方法。
任何想法为什么Kotlin定义Derived
为最终? 有没有办法Derived
不是最终的,所以我可以inheritance它?
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互操作(虽然我没有尝试过)。