package cn.zms.class2
/**
* Created by Lenovo on 2017/6/5.
*/
enum class Direction {
NORTH, SOUTH, WEST, EAST
}
enum class Color(val rgb: Int) {
RED(0xFF0000),
GREEN(0x00FF00),
BLUE(0x0000FF)
}
//Since Kotlin 1.1, it's possible to access the constants in an enum class in a generic way, using the enumValues<T>() and enumValueOf<T>() functions:
inline fun <reified T : Enum<T>> printAllValues() {
print(enumValues<T>().joinToString { it.name })
}
enum class Status {
//匿名类
WAITING {
override fun signal() = TALKING
},
TALKING {
override fun signal() = WAITING
};
abstract fun signal(): Status
}
fun main(args: Array<String>) {
//获取所有类型
for(dr in Direction.values()){
println("${dr.toString()} == ${dr.ordinal}");
}
//获取一个类型,会抛IllegalArgumentException ,如果类型不存在
println(Direction.valueOf("NORTH"))
println(Color.BLUE.rgb)
printAllValues<Color>() // prints RED, GREEN, BLUE
}
Kotlin之枚举类型
最新推荐文章于 2025-02-20 10:21:32 发布