简体   繁体   English

如何从 Kotlin 中的 Java 枚举中获取名称

[英]How to get name from Java enum in Kotlin

I have a project that is written with both Java and Kotlin languages and recently I faced next issue.我有一个用 Java 和 Kotlin 语言编写的项目,最近我遇到了下一个问题。

Let's say we have MyMonth enum:假设我们有MyMonth枚举:

public enum MyMonth {
    JAN("January"),
    FEB("February");

    private final String name;

    MyMonth(final String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

Then in Kotlin when we print the name of the month:然后在 Kotlin 中打印月份名称时:

fun main() {
    val month = MyMonth.JAN
    println(month.name)
}

we get:我们得到:

JAN

which is what is described in the documentation https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-enum/name.html , however actually the name should be January .这是文档https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-enum/name.html中描述的内容,但实际上名称应该是January

Is there a way to get a name in Kotlin that is specified in Java enum?有没有办法在 Kotlin 中获取 Java 枚举中指定的名称?

UPD: My Kotlin version is 1.3.30-release-170 UPD:我的 Kotlin 版本是1.3.30-release-170

UPD2: IDEA even shows me that name is coming from the getName() method defined in Java: UPD2:IDEA 甚至向我展示了名称来自 Java 中定义的getName()方法: 在此处输入图像描述

UPD3: When we explicitly use .getName() it works, however it looks kind of weird UPD3:当我们明确使用.getName()时,它可以工作,但是看起来有点奇怪

Your Java API has name as a private field.您的 Java API 将name作为私有字段。 Nothing can access it, not in Java nor Kotlin.没有任何东西可以访问它,在 Java 和 Kotlin 中都没有。

If you want to access it, add eg the following to the Java API:如果您想访问它,请将以下内容添加到 Java API:

public String getMonthName() { return name; }

...and then access it from Kotlin as ...然后从 Kotlin 访问它

val month = MyMonth.JAN.monthName

You can call the getter directly instead of using the property syntax:您可以直接调用 getter,而不是使用属性语法:

fun main() {
    val name = MyMonth.JAN.getName()
    println(name)
}

You should be calling MyMonth.JAN.name instead. 您应该改为调用MyMonth.JAN.name

Remember, with the Java -> Kotlin interop, any getter in Java like the one you have defined 记住,通过Java-> Kotlin互操作,Java中的任何getter(如您定义的getter)

public String getName() {
    return name;
}

Gets the get dropped when you're calling it from Kotlin. 获取get当你从科特林调用它丢弃。 so when you do MyMonth.JAN.name you aren't accessing the private variable name which you set in the enum. 因此,当您执行MyMonth.JAN.name您不会访问在枚举中设置的私有变量name You are actually accessing the getName() method which you defined 您实际上正在访问您定义的getName()方法

enum is now low usage. enum 现在使用率很低。 You can use IntDef instead of enums.您可以使用 IntDef 代替枚举。

@IntDef(MyMonth.JAN, MyMonth.FEB)
@Retention(AnnotationRetention.SOURCE)
annotation class MyMonth  {
    companion object {
        const val JAN = 0
        const val FEB = 1

        fun getDisplayString(toDoFilterTypes: Int): String {
            return when (toDoFilterTypes) {
                JAN -> "All"
                FEB -> "Job"
                else -> "N/A"
            }
        }
    }
}

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

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