【问题标题】:Does Dart support enumerations?Dart 是否支持枚举?
【发布时间】:2012-12-03 17:00:41
【问题描述】:

Dart 支持枚举吗?例如:

enum myFruitEnum { Apple, Banana }

粗略搜索文档表明没有。

【问题讨论】:

标签: enums dart


【解决方案1】:

1.8开始,你可以像这样使用枚举:

enum Fruit {
  apple, banana
}

main() {
  var a = Fruit.apple;
  switch (a) {
    case Fruit.apple:
      print('it is an apple');
      break;
  }

  // get all the values of the enums
  for (List<Fruit> value in Fruit.values) {
    print(value);
  }

  // get the second value
  print(Fruit.values[1]);
}

1.8之前的老办法:

class Fruit {
  static const APPLE = const Fruit._(0);
  static const BANANA = const Fruit._(1);

  static get values => [APPLE, BANANA];

  final int value;

  const Fruit._(this.value);
}

类中的那些静态常量是编译时常量,这个类现在可以用在例如switch 语句中:

var a = Fruit.APPLE;
switch (a) {
  case Fruit.APPLE:
    print('Yes!');
    break;
}

【讨论】:

  • 使用const 并不总是可行的(如果枚举是用不能是const 的属性构建的)。这就是为什么我没有在我的答案中使用它(尽管我有时在我的代码中使用const 枚举)。
  • 我会接受这个答案,因为在 switch 语句中使用伪枚举类型肯定会很有用
  • @KaiSellgren 注意我认为样式指南现在已经改变,所以枚举值应该是小写的驼峰,而不是全部大写。见dartlang.org/articles/style-guide/…
  • 什么List&lt;Fruit&gt; value?
  • 你可能是想写for (Fruit value in Fruit.values),否则Dart会报错
【解决方案2】:

随着 r41815 Dart 获得原生 Enum 支持,请参阅 http://dartbug.com/21416 并且可以像这样使用

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]
值:Status.none,索引:0
值:Status.running,索引:1
值:Status.stopped,索引:2
值:Status.paused,索引:3
运行:Status.running,1
运行索引:Status.running

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

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

【讨论】:

    【解决方案3】:

    枚举应该在将来可用。但在Enum has landed 之前,您可以执行以下操作:

    class Fruit {
      static final APPLE = new Fruit._();
      static final BANANA = new Fruit._();
    
      static get values => [APPLE, BANANA];
    
      Fruit._();
    }
    

    【讨论】:

      【解决方案4】:

      Thisthis 可能是您问题的答案:

      ... for the technology preview it was decided to leave it out and just 
      use static final fields for now. It may be added later.
      

      你仍然可以这样做:

      interface ConnectionState { }
      class Connected implements ConnectionState { }
      class Connecting implements ConnectionState { }
      class Disconnected implements ConnectionState { }
      
      //later
      ConnectionState connectionState;
      if (connectionState is Connecting) { ... }
      

      我认为使用起来更清楚。编写应用程序结构有点困难,但在某些情况下,它会更好更清晰。

      【讨论】:

      • 我认为对于这个例子,最好省略接口并使用一个类。接口是一个可选的抽象和
      【解决方案5】:

      这个方法怎么样:

      class FruitEnums {
        static const String Apple = "Apple";
        static const String Banana = "Banana";
      }
      
      class EnumUsageExample {
      
        void DoSomething(){
      
          var fruit = FruitEnums.Apple;
          String message;
          switch(fruit){
            case(FruitEnums.Apple):
              message = "Now slicing $fruit.";
              break;
            default:
              message = "Now slicing $fruit via default case.";
              break;
          }
        }
      }
      

      【讨论】:

      • 我自己不会这样做。我会将名称保留为大写Fruit.APPLE。然后,如果我想要文本输出,如果我也想支持其他语言,我将有一个翻译它们的地图或单独的一些语言支持。我还认为switch 语句在整数上效果最好,因为它们可以编译成跳转表。
      【解决方案6】:

      是的!在 Dart 中进行枚举实际上非常有用:

        enum fruits{
          BANANA, APPLE, ORANGE
        }
      

      【讨论】:

      • 小写值是标准值。
      【解决方案7】:

      只需使用类型类文件。

      Dart-Types

      简单、快速、更强大、更有帮助。

      小问题,就是这个类限制为五个不同的选择,加一为空。

      【讨论】:

        猜你喜欢
        • 2011-03-10
        • 1970-01-01
        • 1970-01-01
        • 2021-01-16
        • 1970-01-01
        • 2015-05-12
        • 1970-01-01
        • 2017-04-02
        • 2023-02-07
        相关资源
        最近更新 更多