简体   繁体   English

如何覆盖从 java class 在 kotlin 伴侣 ZA8CFDE6331BD59EB6AC96F8911C4

[英]How to override inherited getter from java class in a kotlin companion object

First of all, Manager (a Kotlin class) inherits from JavaPlugin (which is a Java class).首先, Manager (一个 Kotlin 类)继承自JavaPlugin (这是一个 Java 类)。 This JavaPlugin class contains a getter method getPlugin(Class<T>) .这个 JavaPlugin class 包含一个 getter 方法getPlugin(Class<T>)

Manager class:经理 class:

class Manager : JavaPlugin() {

    companion object {
        [more code...]
        lateinit var plugin: Manager
            private set
    }

    [more code...]

}

Now back in any other Java class (my project is mixed between the two languages) I want to call Manager.getPlugin() , but in Java there is still only the method from the JavaPlugin class, which was inherited. Now back in any other Java class (my project is mixed between the two languages) I want to call Manager.getPlugin() , but in Java there is still only the method from the JavaPlugin class, which was inherited. In Kotlin files, I am able to retrieve plugin variable, but in Java files, I could not figure out how this possible.在 Kotlin 文件中,我能够检索插件变量,但在 Java 文件中,我无法弄清楚这怎么可能。

I know that I could use Manager.Companion.getPlugin() now, but I do not want to change my old Java code (which contains the old static Manager.getPlugin() call).我知道我现在可以使用Manager.Companion.getPlugin() ,但我不想更改旧的 Java 代码(其中包含旧的 static Manager.getPlugin()调用)。

Am I just wrong here with the companion object and should use something different in order to emulate my old static variable with a static getter?我在这里与伴侣 object 是否错了,应该使用不同的东西来模拟我的旧 static 变量与 static 吸气剂? (I read that you could place the variables outside of the class, but I did not get how to access these variables then under the name Manager (it only worked with ManagerKt ).) (我读到您可以将变量放在 class 之外,但我不知道如何在名称Manager下访问这些变量(它仅适用于ManagerKt )。)

I hope someone has a better title for this question and edits it.我希望有人对这个问题有一个更好的标题并对其进行编辑。

Annotate your method @JvmStatic .注释您的方法@JvmStatic

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

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