2024-06-21
This commit is contained in:
50
Writerside/topics/Enum.md
Normal file
50
Writerside/topics/Enum.md
Normal file
@@ -0,0 +1,50 @@
|
||||
# 열거형
|
||||
|
||||
```kotlin
|
||||
enum class Direction {
|
||||
NORTH, SOUTH, EAST, WEST
|
||||
}
|
||||
|
||||
enum class Color(val rgb: Int) {
|
||||
RED(0xff0000),
|
||||
GREEN(0x00ff00),
|
||||
BLUE(0x0000ff)
|
||||
}
|
||||
```
|
||||
|
||||
```kotlin
|
||||
val dir = Direction.valueOf("NORTH")
|
||||
for (item in Direction.values()) {
|
||||
// ...
|
||||
}
|
||||
```
|
||||
|
||||
## 익명 클래스
|
||||
|
||||
```kotlin
|
||||
enum class State {
|
||||
WAITING {
|
||||
override fun signal() = TALKING
|
||||
},
|
||||
TALKING {
|
||||
override fun signal() = WAITING
|
||||
}
|
||||
|
||||
abstract fun signal(): State
|
||||
}
|
||||
```
|
||||
|
||||
## 인터페이스
|
||||
|
||||
```kotlin
|
||||
enum class IntArithmetics : BinaryOperator<Int>, IntBinaryOperator {
|
||||
PLUS {
|
||||
override fun apply(t: Int, u: Int): Int = t + u
|
||||
},
|
||||
TIMES {
|
||||
override fun apply(t: Int, u: Int): Int = t * u
|
||||
};
|
||||
|
||||
override fun applyAsInt(t: Int, u: Int) = apply(t, u)
|
||||
}
|
||||
```
|
||||
Reference in New Issue
Block a user