我可以在Kotlin中使用字符串访问枚举属性吗?
我有这样的区域的枚举:
enum class Regions(val location:String){
REGION_1("London"),
}
有没有一种方法可以使用以下功能中的字符串访问区域_1的属性?
fun access(name:String){
return Regions.<modifed_name>.location
}
I have an enum of regions like this:
enum class Regions(val location:String){
REGION_1("London"),
}
Is there a way to access the properties of region_1 with just a string like in the below function?
fun access(name:String){
return Regions.<modifed_name>.location
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用valueof(value:string)将字符串转换为枚举,然后使用该位置
You can convert your string to enum using valueOf(value: string) and then use the location