如何在Kotlin中初始化一个空的枚举?
我是Kotlin的新手,并尝试创建一种与状态相对应的怪物枚举,我可以通过切换各个位来切换单个状态。但是我陷入了对象没有状态的位置。
import java.util.*
fun main() {
var d = Door(100, EnumSet.noneOf(Status))
}
enum class Status(intValue: Int) {
OPEN(1),
CLOSED(1 shl 1),
CRACKED(1 shl 2),
BROKEN(1 shl 3)
}
class Door(val id: Int, val status: EnumSet<Status>) {}
此代码因错误classifier'状态而失败'没有伴随对象,因此必须在此处初始化
我正在阅读另一个问题,他们说要使用status.class
enumset.noneof
,但这会带来更多的错误。
I'm pretty new to Kotlin and trying to create a sort of bitset enum where ints correspond to state and I can toggle individual states by toggling individual bits. But I'm stuck on where an object can have no state.
import java.util.*
fun main() {
var d = Door(100, EnumSet.noneOf(Status))
}
enum class Status(intValue: Int) {
OPEN(1),
CLOSED(1 shl 1),
CRACKED(1 shl 2),
BROKEN(1 shl 3)
}
class Door(val id: Int, val status: EnumSet<Status>) {}
This code fails with the error Classifier 'Status' does not have a companion object, and thus must be initialized here
I was reading another question where they said to use Status.class
inside the EnumSet.noneOf
, but that throws even more errors.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
status :: class
与java中的status.class
相似,但是获得了一个Kotlinkotlin.reflect.reflect.kclass.kclass
。要获取javajava.lang.class
enumset.noneof
接受,您可以访问java
属性。Use
Status::class
is similar toStatus.class
in Java, but gets a Kotlinkotlin.reflect.KClass
instead. To get the Javajava.lang.Class
thatEnumSet.noneOf
takes, you access thejava
property.