枚举可以是可变的,这是获得单例行为的另一种方法:
enum class Planet(var population: Int = 0) { EARTH(7 * 100000000), MARS(); override fun toString() = "$name[population=$population]" } println(Planet.MARS) // 火星[人口= 0] Planet.MARS.population = 3 println(Planet.MARS) // 火星[人口= 3]