Dart 枚举2025 年 3 月 19 日 | 2 分钟阅读 枚举是一组称为元素、成员等的值。当我们在变量可用的值有限的情况下进行操作时,这一点至关重要。例如 - 您可以认为月份中的天数只能是七天中的一天 - 星期日、星期一、星期二、星期三、星期四、星期五、星期六。 初始化枚举枚举使用 enum 关键字声明,后跟有效标识符的逗号分隔列表。此列表用花括号 {} 括起来。语法如下所示。 语法 - 在这里,enum_name 表示枚举类型名称,标识符列表用花括号括起来。 枚举列表中的每个标识符都有其索引位置。第一个枚举的索引为 0;第二个枚举为 1,依此类推。 示例 - 让我们为一年中的月份定义一个枚举。 举个编程例子 - 示例 - 1 输出 Tpoint Tech - Dart Enumeration [EnumofYear.January, EnumofYear.February, EnumofYear.March, EnumofYear.April, EnumofYear.May, EnumofYear.June, EnumofYear.July, EnumofYear.August, EnumofYear.September, EnumofYear.October, EnumofYear.November, EnumofYear.December] value: EnumofYear.January, index: 0 value: EnumofYear.February, index: 1 value: EnumofYear.March, index: 2 value: EnumofYear.April, index: 3 value: EnumofYear.May, index: 4 value: EnumofYear.June, index: 5 value: EnumofYear.July, index: 6 value: EnumofYear.August, index: 7 value: EnumofYear.September, index: 8 value: EnumofYear.October, index: 9 value: EnumofYear.November, index: 10 value: EnumofYear.December, index: 11 示例 - 2 输出 [Process_Status.none, Process_Status.running, Process_Status.stopped, Process_Status.paused] value: Process_Status.none, index: 0 value: Process_Status.running, index: 1 value: Process_Status.stopped, index: 2 value: Process_Status.paused, index: 3 running: Process_Status.running, 1 running index: Process_Status.running 下一主题Dart 控制流语句 |
我们请求您订阅我们的新闻通讯以获取最新更新。