本文介绍了枚举中的“名称"声明冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个简单的枚举类,我想在其中添加一个名为 name 的字段.
I have a simple enum class in which I would like to have a field called name.
enum class DeviceFieldQuery(val clazz: Class<*>) {
id(Int::class.java),
name(String::class.java),
}
不幸的是,这在Kotlin中似乎不起作用.编译失败,并显示以下消息:
Unfortunately, this does not seem to work in Kotlin. Compilation fails with the message:
与Java代码相同的Enum类可以正常工作.我该如何在Kotlin中解决这个问题?
The same Enum class as Java code works fine. How may I solve this in Kotlin?
推荐答案
Kotlin中的枚举已经定义了name
属性(例如Java).这与名为name
的枚举冲突.要解决此问题,您可以将其大写,这是比较惯用的:
Enums in Kotlin already have a name
property already defined (like Java). This is conflicting with your enum called name
. To fix it, you could capitalize it, which is more idiomatic:
enum class DeviceFieldQuery(val clazz: Class<*>) {
Id(Int::class.java),
Name(String::class.java),
}
这篇关于枚举中的“名称"声明冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!