Dart는 열거를 지원합니까?
Dart는 열거를 지원합니까? 예를 들면 :
enum myFruitEnum { Apple, Banana }
문서에 대한 간단한 검색은 아니오를 제안합니다.
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;
}
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
A limitation is that it is not possibly to set custom values for an enum item, they are automatically numbered.
More details at in this draft https://www.dartlang.org/docs/spec/EnumsTC52draft.pdf
This and this may be the answers on your question:
... for the technology preview it was decided to leave it out and just
use static final fields for now. It may be added later.
You can still do something like this:
interface ConnectionState { }
class Connected implements ConnectionState { }
class Connecting implements ConnectionState { }
class Disconnected implements ConnectionState { }
//later
ConnectionState connectionState;
if (connectionState is Connecting) { ... }
wich is in my opinion more clear for use. It's a bit more difficult for programming the application structure, but in some cases, it's better and clear.
Enumeration should be available in the future. But until Enum has landed you can do something like :
class Fruit {
static final APPLE = new Fruit._();
static final BANANA = new Fruit._();
static get values => [APPLE, BANANA];
Fruit._();
}
how about this approach:
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;
}
}
}
참고URL : https://stackoverflow.com/questions/13899928/does-dart-support-enumerations
'Program Tip' 카테고리의 다른 글
HH : MM : SS 문자열을 자바 스크립트에서만 초로 변환 (0) | 2020.10.22 |
---|---|
구문 분석 중 Python 예기치 않은 EOF (0) | 2020.10.21 |
WPF MVVM 직선 XAML 창보기가 아닌 ContentControl + DataTemplate보기를 사용하는 이유는 무엇입니까? (0) | 2020.10.21 |
app.yaml을 사용하여 GAE에 환경 변수를 안전하게 저장 (0) | 2020.10.21 |
PyCharm 내에서 (Ana) conda 사용 (0) | 2020.10.21 |