简体   繁体   English

Java class如何扩展Kotlin库

[英]How can Java class extend Kotlin library

How can Java class extend Kotlin library? Java class 如何扩展 Kotlin 库?

I understand what all kotlin classes are final.我了解所有 kotlin 类都是最终类。

I have library and I need to override some methods.我有库,我需要重写一些方法。 But then I extend library, I'm getting error但是后来我扩展了库,我收到了错误

public class Editor extends EditorLibrary //error Cannot inherit from final

Any chances to extend kotlin final class, because I can't change it to open?是否有机会延长 kotlin 最终 class,因为我无法将其更改为打开?

If you have control over the library you can open the class for extension using the open keyword, otherwise, as you've mentioned, the class was not built for extensions...如果您可以控制库,则可以使用open关键字打开 class 进行扩展,否则,正如您所提到的,class 不是为扩展而构建的......

open EditorLibrary(...) {
}

In such case you might consider using composition over inheritance... eg :在这种情况下,您可能会考虑在 inheritance 上使用合成...例如:

public class Editor {
    private final EditorLibrary editor;
    
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM