从Java访问Kotlin类对象 [英] Accessing Kotlin class object from Java
问题描述
我有一个Kotlin类,它有一个类对象,例如
I have a Kotlin class which has a class object, e.g.
public class Foo {
public class object {
public val SomeValue : Int = 0
}
}
如果我从Java使用此类,如何访问类对象内的SomeValue?如果它是具有静态属性的Java类,则只使用Foo.SomeValue-但在这里不能这样做.
If I'm using this class from Java, how do I access SomeValue inside the class object? If it were a Java class with a static property, I'd just use Foo.SomeValue - but I can't do that here.
IntellIJ显示我可以访问Foo.object.$instance
,但是$instance
没有getSomeValue或类似的内容.无论如何,如果我尝试使用$ instance.SomeValue,则在生成错误消息时会说:
IntellIJ shows that I can access Foo.object.$instance
, but $instance
doesn't have getSomeValue or anything like that. If I try to use $instance.SomeValue anyway, when I build the error message says:
SomeValue在Foo.object中具有私有访问权限
SomeValue has private access in Foo.object
我正在使用Kotlin 0.5.1.
I'm using Kotlin 0.5.1.
推荐答案
getSomeValue()的缺失"是IDE中的一个错误.如果使用它,它将编译确定. 我创建了一个问题: http://youtrack.jetbrains.com/issue/KT-3337
The "absense" of getSomeValue() is a bug in the IDE. If you use it, it compiles OK. I created an issue: http://youtrack.jetbrains.com/issue/KT-3337
这篇关于从Java访问Kotlin类对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!