枚举

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

使用enum关键字声明一个枚举类型:

enum Color { red, green, blue }

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

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

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

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

公众号“编程之路从0到1”

20190301102949549

Copyright © Arcticfox 2020 all right reserved,powered by Gitbook文档修订于: 2024-06-09 20:22:55

results matching ""

    No results matching ""