상세 컨텐츠

본문 제목

Class - enum

Dart

by techbard 2024. 1. 19. 11:24

본문

반응형
void main(List<String> args) {
  print("Index of pending: ${Status.pending.index}");
  print("Index of pending: ${Status.rejected.index}");

  // To get a list of all enumerated values you use the values constants of the enum like this:

  print("");
  print("---List of enum values---");
  List<Status> statuses = Status.values;
  for (var status in statuses) {
    print(status);
  }

  // To access the name of an enumerated value, you use the .name property.
  print("");
  print("---List of enum names---");
  for (var status in statuses) {
    print(status.name);
  }
}

enum Status { pending, completed, rejected }

// 결과
Index of pending: 0
Index of pending: 2

---List of enum values---
Status.pending
Status.completed
Status.rejected

---List of enum names---
pending
completed
rejected

 

// Enhanced enums

/*
If you need to use enhanced enums. 
Like classes, you can define fields, methods, 
and const constructors with the following rules:

- Instance variables must be final. 
They cannot have the name values because it’ll cause 
a conflict with the autogenerated values getter.
- Generative constructors must be constant.
- Factory constructors can only return one of the fixed, known enum instances.
- Cannot override index, hashCode, the equality operator ==.
- Declare all instances at the beginning of the enum. An enum must have at least one instance.
*/

enum Numbers {
  one(number: 1),
  two(number: 2),
  three(number: 3);

  final int number;
  const Numbers({required this.number});

  bool operator <(Numbers n) => number < n.number;
  bool operator >(Numbers n) => number > n.number;
}

void main(List<String> args) {
  var num = Numbers.one;
  if (num < Numbers.three) {
    print("Numbers.three is bigger than Numbers.one");
  }

  if (Numbers.one < Numbers.two) {
    print("Num two > Num one.");
  }

  if (Numbers.three > Numbers.two) {
    print("Num three > Num two");
  }
}

// 결과
Numbers.three is bigger than Numbers.one
Num two > Num one.
Num three > Num two

 

enum Medal { gold, silver, bronze, noMedal }

/*
enum types carry more meaning
  - explicitly define allowed values
  - resulting code is more readable
*/

void main(List<String> args) {
  var medal = Medal.noMedal;

  switch (medal) {
    case Medal.gold:
      print('gold 🤗');
      break;
    case Medal.silver:
      print('silver 😃');
      break;
    case Medal.bronze:
      print('bronze 😐');
      break;
    case Medal.noMedal:
      print('noMedal 😞');
      break;
  }
}

// 결과
noMedal 😞

 

void main(List<String> args) {}

enum Day {
  monday,
  tuesday,
  wednesday,
  thursday,
  friday,
  saturday,
  sunday,
}

/*
Enumerations (enums)

An enum is a user-defined type consisting of a set of named constants called
enumerators.

If you use enums instead of integers (or String codes), you increase compile-time
checking and avoid errors from passing in invalid constants, and you document
which values are legal to use.
*/
반응형

관련글 더보기

댓글 영역