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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38120494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 03:08:31  来源:igfitidea点击:

How to access Kotlin companion object in Java?

javakotlin

提问by Elye

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. But not when MainApplication in Kotlin, the above code error

当 MainApplication 在 Java 中时很好。但是在Kotlin中MainApplication的时候不行,上面的代码报错

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

How could I access getin my Java code above?

我如何get在上面的 Java 代码中访问?

回答by Marcin Koziński

You can add @JvmStaticannotation 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: @JvmStaticdoesn't actually movewhere the method gets generated. It duplicatesit, by generating a static method for Java in addition to the method on the companion object. Personally I think this isn't great and it can have some implications depending on a use case, so something worth knowing.

编辑:我觉得有必要添加一些我最近学到的东西:@JvmStatic实际上并没有移动到生成方法的地方。它复制它,通过在除了同伴对象上的方法生成的Java的静态方法。我个人认为这不是很好,它可能会根据用例产生一些影响,因此值得了解。

回答by Elye

Ops, I got it. Just use the below.

哦,我明白了。只需使用下面的。

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

回答by Mike

By omitting the name of your companion object, the name Companionmust 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 Objectsfor details.

有关详细信息,请参阅关于伴随对象的 Kotlin 文档。

回答by Dmitriy Pavlukhin

You may encounter a problem where you cannot access the Companion object's method in Java if the newkeyword is used in the method call. The newkeyword should be omitted. The documentationstates:

如果new在方法调用中使用了关键字,您可能会遇到Java 中无法访问Companion 对象的方法的问题。该new关键字应该被忽略。该文件规定:

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()

MyClass.create()

But not like this:

但不是这样:

new MyClass.create

new MyClass.create