相关疑难解决方法(0)

将静态变量从Java转换为Kotlin

我正在尝试将以下代码转换为Kotlin,并且仍然使用Java使用的一个类(Foo).进行此转换的正确方法是什么?

原始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();
    }
}
Run Code Online (Sandbox Code Playgroud)

自动转换Foo到Kotlin

object Foo {
    val C_ID = "ID"
    val C_NAME = "NAME"
    val VALUES = arrayOf("X", "Y", …
Run Code Online (Sandbox Code Playgroud)

static field properties kotlin

25
推荐指数
2
解决办法
2万
查看次数

标签 统计

field ×1

kotlin ×1

properties ×1

static ×1