Tutorial by Examples

Enum classes as any other classes can have a constructor and be initialized enum class Color(val rgb: Int) { RED(0xFF0000), GREEN(0x00FF00), BLUE(0x0000FF) }
Enum classes can also declare members (i.e. properties and functions). A semicolon (;) must be placed between the last enum object and the first member declaration. If a member is abstract, the enum objects must implement it. enum class Color { RED { override val rgb: Int = 0xFF0000 ...
enum class Color { RED, GREEN, BLUE } Each enum constant is an object. Enum constants are separated with commas.
Enums can be mutable, this is another way to obtain a singleton behavior: enum class Planet(var population: Int = 0) { EARTH(7 * 100000000), MARS(); override fun toString() = "$name[population=$population]" } println(Planet.MARS) // MARS[population=0] Planet.MARS.p...

Page 1 of 1