英文:
Two enum constants in Kotlin with the same value
问题
在Kotlin中是否可以为枚举常量创建一个'alias'(别名),以便可以通过两个名称引用同一个枚举常量?
例如:如果MyEnum
是一个包含两个常量A1
和A2
的枚举的名称,那么MyEnum.B1
应该与MyEnum.A1
实际上是相同的,MyEnum.B2
应该与MyEnum.A2
实际上是相同的。
英文:
Is it possible to create an 'alias' for an enum constant in Kotlin, so that its possible to refer to one single enum constant by two names?
Eg: If MyEnum
is the name of the enum with 2 constants A1
and A2
, then MyEnum.B1
should effectively be the same as MyEnum.A1
and MyEnum.B2
should effectively be the same as MyEnum.A2
.
答案1
得分: 1
你可以通过在枚举内部将 'alias' 名称作为静态 val
包含来简单实现此操作。
enum class MyEnum {
A1, A2;
companion object {
val B1 = A1
val B2 = A2
}
}
英文:
You can do this simply by including the 'alias' names as static val
s inside the enum.
enum class MyEnum {
A1, A2;
companion object {
val B1 = A1
val B2 = A2
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论