我正在移植一个大量基于 Java 枚举的库,并且需要编写我自己的枚举,直到有对它们的本机支持。
然而我失败了!
在 ChessColor.values() 方法下面的代码中返回 null,我不明白为什么。
但是我是 Dart 的新手...
一定有一些我错过了静态字段和初始化的东西......
枚举基类
part of chessmodel;
/**
* Emulation of Java Enum class.
*/
abstract class Enum {
final int code;
final String name;
Enum(this.code, this.name);
toString() => name;
}
一个简单的使用尝试
part of chessmodel;
final ChessColor WHITE = ChessColor.WHITE;
final ChessColor BLACK = ChessColor.BLACK;
class ChessColor extends Enum {
static List<ChessColor> _values;
static Map<String, ChessColor> _valueByName;
static ChessColor WHITE = new ChessColor._x(0, "WHITE");
static ChessColor BLACK = new ChessColor._x(1, "BLACK");
ChessColor._x(int code, String name) : super (code, name) {
if (_values == null) {
_values = new List<ChessColor>();
_valueByName = new Map<String, ChessColor>();
}
_values.add(this);
_valueByName[name] = this;
}
static List<ChessColor> values() {
return _values;
}
static ChessColor valueOf(String name) {
return _valueByName [name];
}
ChessColor opponent() {
return this == WHITE ? BLACK : WHITE;
}
bool isWhite() {
return this == WHITE;
}
bool isBlack() {
return this == BLACK;
}
}