将静态变量从Java转换为Kotlin [英] Convert static variables from Java to Kotlin
问题描述
我正在尝试将以下代码转换为Kotlin,并且仍然具有Java使用的类之一(Foo).进行此转换的正确方法是什么?
I'm trying to convert the following code to Kotlin AND still have one of the classes (Foo) used by Java. What is the proper way of making this conversion?
原始Java:
public class Foo {
public static final String C_ID = "ID";
public static final String C_NAME = "NAME";
public static final String[] VALUES = {"X", "Y", "Z"};
public static String[] getAll() {
return new String[] {C_ID, C_NAME};
}
}
public class Bar {
public void doStuff() {
String var1 = Foo.C_ID;
String[] array1 = Foo.VALUES;
String[] array2 = Foo.getAll();
}
}
将fo Foo自动转换为Kotlin
Auto conversion fo Foo to Kotlin
object Foo {
val C_ID = "ID"
val C_NAME = "NAME"
val VALUES = arrayOf("X", "Y", "Z")
val all: Array<String>
get() = arrayOf(C_ID, C_NAME)
}
问题:
Bar类不能再访问C_ID或VALUES(错误:私有访问")
Bar class can no longer access C_ID or VALUES (error: "private access")
如果我将"const"放在C_ID的前面,它可以工作...但是我不能对VALUES做同样的事情("const"只能用于基元或字符串)
if I put "const" in front of C_ID, it works... but I cannot do the same with VALUES ("const" can ONLY be used on primatives or String)
我应该采用其他方式(这样Java代码和Kotlin代码都可以访问Foo中的所有内容)吗?
Is there a different way I should be doing this (so both Java code and Kotlin code can access everything in Foo)?
推荐答案
当前语义来自
我们已经使生成纯字段(与 We have made the strategy for generating pure fields (as opposed to 此外,现在可以通过名称 Also, singleton instances are now accessible by the name 据此以及参考,有三种使用Java Kotlin According to this and to the reference, there are three ways of working with properties of a Kotlin 使用 默认情况下, By default, properties of 因此表达式将为 使用 这将为 This will generate static getter for 在属性声明中使用 Use 这将使Kotlin编译器为Java生成一个静态字段而不是属性.
然后,在Java中,您可以将其作为静态字段进行访问: This will make Kotlin compiler generate a static field for Java instead of property.
Then in Java you can access it as a static field: 但是如果不支持示例中的 But it won't work on properties without backing fields like 对于原语,正如您所说,可以使用 For primitives, as you stated, one can use 顺便说一句,当涉及到方法时,情况是相同的,并且为它们提供了 By the way, when it comes to methods, the situation is the same, and there is 这篇关于将静态变量从Java转换为Kotlin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! @JvmField和对象
get
/set
对相对)的策略更加可预测:从现在开始,只有标注为@JvmField
,lateinit
或const
的属性才作为字段公开Java客户端.较早的版本使用启发式方法并无条件地在对象中创建静态字段,这违背了我们最初的设计目标,即默认情况下具有二进制兼容性友好的API.
@JvmField and objects
get
/set
pairs) more predictable: from now on only properties annotated as @JvmField
, lateinit
or const
are exposed as fields to Java clients. Older versions used heuristics and created static fields in objects unconditionally, which is against our initial design goal of having binary-compatibility-friendly APIs by default.INSTANCE
(而不是INSTANCE$
)访问单例实例.INSTANCE
(instead of INSTANCE$
).object
属性的方法:object
from Java:
Foo.INSTANCE
.object
的属性对于Java而言不是静态字段,但是Java可以通过Foo
对象实例-Foo.INSTANCE
访问这些属性.object
won't be static fields for Java, but Java can access the properties through Foo
object instance -- Foo.INSTANCE
.Foo.INSTANCE.getC_ID()
.@JvmStatic
批注标记属性:object Foo {
@JvmStatic val C_ID = "ID"
//...
}
C_ID
生成静态吸气剂,而不是作为Foo.getC_ID()
可以访问的Foo
实例吸气剂.C_ID
instead of Foo
instance getter which will be accessible as Foo.getC_ID()
.@JvmField
批注:@JvmField
annotation on property declaration:object Foo {
@JvmField val C_ID = "ID"
//...
}
Foo.C_ID
.Foo.C_ID
.all
这样的字段,它将无法在属性上工作.all
in your example.const
,就Java的可见性而言,其作用与@JvmField
相同.const
which will have the same effect as @JvmField
in terms of visibility in Java.@JvmStatic
注释.@JvmStatic
annotation for them.