枚举类型

Last updated: ... / Reads: 31 Edit

枚举类型通常称为枚举或枚举,是一种特殊的类,用于表示固定数量的常量值。

  • 所有枚举都会自动扩展 Enum 类。它们也是密封的,这意味着它们不能被子类化、实现、混合或以其他方式显式实例化。
  • 抽象类和 mixin 可以显式实现或扩展 Enum ,但除非它们随后由枚举声明实现或混合到枚举声明中,否则没有对象可以实际实现该类或 mixin 的类型。

声明简单枚举

要声明简单枚举类型,请使用 enum 关键字并列出要枚举的值:

enum Color { red, green, blue }

您还可以在声明枚举类型时使用尾随逗号,以帮助防止复制粘贴错误。

声明增强枚举

Dart 还允许枚举声明来声明具有字段、方法和 const 构造函数的类,这些构造函数仅限于固定数量的已知常量实例。

要声明增强枚举,请遵循与普通类类似的语法,但有一些额外的要求:

  • 实例变量必须是 final ,包括由 mixins 添加的变量。
  • 所有生成构造函数都必须是常量。
  • 工厂构造函数只能返回固定的已知枚举实例之一。
  • 无法扩展其他类,因为 Enum 会自动扩展。
  • 不能覆盖 index 、 hashCode 、相等运算符 == 。
  • 不能在枚举中声明名为 values 的成员,因为它会与自动生成的静态 values getter 冲突。
  • 枚举的所有实例都必须在声明的开头声明,并且必须至少声明一个实例。

增强枚举中的实例方法可以使用 this 来引用当前枚举值。

下面是一个示例,它声明了一个具有多个实例、实例变量、getter 和实现的接口的增强枚举:

enum Vehicle implements Comparable<Vehicle> {
  car(tires: 4, passengers: 5, carbonPerKilometer: 400),
  bus(tires: 6, passengers: 50, carbonPerKilometer: 800),
  bicycle(tires: 2, passengers: 1, carbonPerKilometer: 0);

  const Vehicle({
    required this.tires,
    required this.passengers,
    required this.carbonPerKilometer,
  });

  final int tires;
  final int passengers;
  final int carbonPerKilometer;

  int get carbonFootprint => (carbonPerKilometer / passengers).round();

  bool get isTwoWheeled => this == Vehicle.bicycle;

  @override
  int compareTo(Vehicle other) => carbonFootprint - other.carbonFootprint;
}

增强枚举需要至少 2.17 的语言版本。

使用枚举

像任何其他静态变量一样访问枚举值:

final favoriteColor = Color.blue;
if (favoriteColor == Color.blue) {
  print('Your favorite color is blue!');
}

枚举中的每个值都有一个 index getter,它返回枚举声明中值从零开始的位置。例如,第一个值的索引为 0,第二个值的索引为 1。

assert(Color.red.index == 0);
assert(Color.green.index == 1);
assert(Color.blue.index == 2);

要获取所有枚举值的列表,请使用枚举的 values 常量。

List<Color> colors = Color.values;
assert(colors[2] == Color.blue);

您可以在 switch 语句中使用枚举,如果您不处理所有枚举的值,您将收到警告:

var aColor = Color.blue;

switch (aColor) {
  case Color.red:
    print('Red as roses!');
  case Color.green:
    print('Green as grass!');
  default: // Without this, you see a WARNING.
    print(aColor); // 'Color.blue'
}

如果您需要访问枚举值的名称,例如 Color.blue 中的 'blue' ,请使用 .name 属性:

print(Color.blue.name); // 'blue'

您可以像访问普通对象一样访问枚举值的成员:

print(Vehicle.car.carbonFootprint);

Comments

Make a comment