-
Notifications
You must be signed in to change notification settings - Fork 1
/
EnumClasses.kt
54 lines (41 loc) · 1.36 KB
/
EnumClasses.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
class EnumClasses {
enum class Direction {
NORTH, SOUTH, WEST, EAST
}
enum class Color(val rgb: Int) {
RED(0xFF0000),
GREEN(0x00FF00),
BLUE(0x0000FF)
}
enum class ProtocolState {
WAITING {
override val id: Int
get() = 1
override fun signal(): ProtocolState = WAITING
},
TALKING {
override val id: Int
get() = 2
override fun signal(): ProtocolState = TALKING
};
abstract val id: Int
abstract fun signal(): ProtocolState
}
enum class RGB { RED, GREEN, BLUE }
private inline fun <reified T : Enum<T>> printAllValues() {
print(enumValues<T>().joinToString { it.name })
}
fun test() {
println("enum 1") // Basic usage of enum classes
println("Direction.NORTH = ${Direction.NORTH}")
println("")
println("enum 2") // Another way to initialse the enum
println("Color = ${Color.RED}(${Integer.toHexString(Color.RED.rgb)}")
println("")
println("Anonymous Classes") // Enum constants can also declare their own anonymous classes
println("ProtocolState = ${ProtocolState.TALKING.signal()}(id = ${ProtocolState.TALKING.id})")
println("")
println("Enum Constants")
printAllValues<RGB>()
}
}