In Kotlin, I'm unable to reference the instances of an enum directly when E is in the same file as the code where I use its instances:
enum class E {
A, B
}
What I want to do:
val e = A
What I can do:
val e = E.A
这可能吗?
In Kotlin, I'm unable to reference the instances of an enum directly when E is in the same file as the code where I use its instances:
enum class E {
A, B
}
What I want to do:
val e = A
What I can do:
val e = E.A
这可能吗?
在Kotlin中,可以像导入大多数其他实例一样导入枚举实例,因此假设enum类E在默认包中,您只需在希望直接使用其实例的源文件的顶部添加import E.*
即可.例如:
import E.*
val a = A // now translates to E.A
每个实例也可以单独导入,而不仅仅是导入枚举中的所有内容:
import E.A
import E.B
//etc...
即使在同一个文件中声明了枚举,这也会起作用:
import E.*
enum class E{A,B}
val a = A