简体   繁体   English

如何在 Java 中访问 Kotlin 伴侣对象?

[英]How to access Kotlin companion object in Java?

I convert one of my Java class to Kotlin and the class as below.我将我的 Java 类之一转换为 Kotlin 和如下所示的类。

class MainApplication : Application() {
    companion object {
        operator fun get(context: Context): MainApplication {
            return context.applicationContext as MainApplication
        }
    }
}

It has a static function get .它有一个静态函数get

I still have a Java function accessing it.我仍然有一个 Java 函数可以访问它。

MainApplication application = MainApplication.get(mContext);

It was good when MainApplication is in Java.当 MainApplication 在 Java 中时很好。 But not when MainApplication in Kotlin, the above code error但是在Kotlin中MainApplication的时候不行,上面的代码报错

Error:(27, 54) error: cannot find symbol method get(Context)

How could I access get in my Java code above?我如何访问上面的 Java 代码中的get

You can add @JvmStatic annotation to the method in companion object to make Kotlin generate a static method.您可以在伴随对象中的方法中添加@JvmStatic注解,使 Kotlin 生成静态方法。

class MainApplication : Application() {
    companion object {
        @JvmStatic fun get(context: Context): MainApplication {
            return context.applicationContext as MainApplication
        }
    }
}

you can then access it from Java like before converting to Kotlin:然后,您可以像在转换为 Kotlin 之前一样从 Java 访问它:

MainApplication application = MainApplication.get(mContext);

EDIT: I feel obliged to add something I learned recently: @JvmStatic doesn't actually move where the method gets generated.编辑:我觉得有必要添加一些我最近学到的东西: @JvmStatic实际上并没有移动到生成方法的位置。 It duplicates it, by generating a static method for Java in addition to the method on the companion object.除了伴随对象上的方法之外,它还通过为 Java 生成静态方法来复制它。 Personally I think this isn't great and it can have some implications depending on a use case, so something worth knowing.我个人认为这不是很好,它可能会根据用例产生一些影响,因此值得了解。

Ops, I got it.哦,我明白了。 Just use the below.只需使用下面的。

MainApplication application = MainApplication.Companion.get(mContext);

By omitting the name of your companion object, the name Companion must be used to access the methods.通过省略伴随对象的名称,必须使用名称Companion来访问方法。

Example:示例:

class MyClass1 {
    companion object Object1 {
        fun method1 {
        }
    }
}

class MyClass2 {
    companion object {
        fun method2 {
        }
    }
}

To invoke the first companion object method you would do the following:要调用第一个伴随对象方法,您将执行以下操作:

MyClass1.method1()

To invoke the second:调用第二个:

MyClass2.Companion.method2()

See the Kotlin docs on Companion Objects for details.有关详细信息,请参阅关于伴随对象的 Kotlin 文档。

You may encounter a problem where you cannot access the Companion object's method in Java if the new keyword is used in the method call.如果在方法调用中使用了new关键字,您可能会遇到 Java 中无法访问 Companion 对象的方法的问题。 The new keyword should be omitted.应该省略new关键字。 The documentation states:文件指出:

Companion objects and their members can only be accessed via the containing class name, not via instances of the containing class.伴生对象及其成员只能通过包含类名访问,而不能通过包含类的实例访问。

So if you have a class like this:因此,如果您有这样的课程:

class MyClass {
    companion object {
        fun create() {}
    }
}

You can call the companion object's method like this:您可以像这样调用伴随对象的方法:

MyClass.create()

But not like this:但不是这样:

new MyClass.create

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

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