如何使用Dart构建枚举? [英] How can I build an enum with Dart?

查看:1984
本文介绍了如何使用Dart构建枚举?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Dart语言没有枚举(但是)。

The Dart language does not have enums (yet??). What is the proper or idiomatic way to construct an enum, at least until a language feature arrives?

推荐答案

使用r41815 Dart得到了原生的枚举支持请参见 http://dartbug.com/21416 ,并可以使用

With r41815 Dart got native Enum support see http://dartbug.com/21416 and can be used like

enum Status {
  none,
  running,
  stopped,
  paused
}

void main() {
  print(Status.values);
  Status.values.forEach((v) => print('value: $v, index: ${v.index}'));
  print('running: ${Status.running}, ${Status.running.index}');
  print('running index: ${Status.values[1]}');
}




[Status.none,Status.running, Status.stopped,Status.paused]

value:Status.none,index:0

value:Status.running,index:1

value:Status .stopped,index:2

value:Status.paused,index:3

running:Status.running,1

running index:Status.running

[Status.none, Status.running, Status.stopped, Status.paused]
value: Status.none, index: 0
value: Status.running, index: 1
value: Status.stopped, index: 2
value: Status.paused, index: 3
running: Status.running, 1
running index: Status.running

限制是不能为枚举项设置自定义值,它们会自动编号。

A limitation is that it is not possibly to set custom values for an enum item, they are automatically numbered.

本草案中的更多详情,请参阅 https:// www。 dartlang.org/docs/spec/EnumsTC52draft.pdf

More details at in this draft https://www.dartlang.org/docs/spec/EnumsTC52draft.pdf

这篇关于如何使用Dart构建枚举?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆