37 普通枚举
枚举列出有限个具名选项。
norm
enum State {
Ready,
Running,
Done
}
main() {
State current = State.Ready
printLine(current == State.Ready)
current = State.Done
printLine(current == State.Done)
}预期输出:
text
true
trueState.Ready 与 State.Done 是同一类型的不同变体。变量可以改变所持有的变体,而枚举声明限定了全部可能状态。
动手试试:加入 Paused 变体,并在 main 中赋值。
详细规则:参考。