Kotlin之枚举类型

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() and enumValueOf() functions:
inline fun > printAllValues() {
    print(enumValues().joinToString { it.name })
}
enum class Status {
    //匿名类
    WAITING {
        override fun signal() = TALKING
    },

    TALKING {
        override fun signal() = WAITING
    };

    abstract fun signal(): Status

}

fun main(args: Array) {
    //获取所有类型
    for(dr in Direction.values()){
        println("${dr.toString()} == ${dr.ordinal}");
    }
    //获取一个类型,会抛IllegalArgumentException ,如果类型不存在
    println(Direction.valueOf("NORTH"))

    println(Color.BLUE.rgb)
    printAllValues() // prints RED, GREEN, BLUE
}

你可能感兴趣的:(Kotlin)