我可以用Java扩展Kotlin委托类吗?

时间:2017-07-18 11:47:53

标签: java kotlin decorator delegation

我正在尝试使用Java扩展Kotlin委托类并获得以下错误:

  

无法继承最终的'衍生'

见下面的代码。

我要做的是装饰一个类的方法。

知道为什么Kotlin将SELECT DISTINCT t1.col1, t2.col2, t3.col3 FROM mytable AS t1 -- col1 is realted to col2 LEFT JOIN mytable AS t2 ON t1.col1 = t2.col1 AND t2.col2 IS NOT NULL -- col1 is related to col3 LEFT JOIN mytable AS t3 ON t1.col1 = t3.col1 AND t3.col3 IS NOT NULL WHERE t1.col1 IS NOT NULL -- col2 is related to col3 AND EXISTS (SELECT 1 FROM mytable AS t4 WHERE t4.col2 = t2.col2 AND t4.col3 = t3.col3); 定义为最终版?有没有办法让Derived不是最终的,所以我可以继承它?

爪哇:

Derived

科特林:

new Derived(new BaseImpl(10)) { // Getting the error on this line: `Cannot inherit from final 'Derived'`
};

*示例来自:https://kotlinlang.org/docs/reference/delegation.html

1 个答案:

答案 0 :(得分:9)

Kotlin classes are final in by default。将类标记为open以扩展它(无论是来自Kotlin还是来自Java):

open class Derived(b: Base) : Base by b

这是委托类的事实应该对Java互操作没有影响(虽然我没有尝试过)。