[Kotlin] Enum class

enum class Color {
    RED,
    GREEN,
    BLUE
}

fun main() {
    println(Color.GREEN) // GREEN
}

  Or give enum a value:

enum class Color {
    RED(0xFF0000),
    GREEN(0x00FF00),
    BLUE(0x0000FF)
}

 

Give a method:

enum class Color(val rgb: Int) {
    RED(0xFF0000),
    GREEN(0x00FF00),
    BLUE(0x0000FF); // need ; split enum and fun

     fun containRed() {
        return this.rgb and 0xFF0000 != 0
     }  
}

// Color.RED.containRed() // true

 

posted @ 2020-10-12 15:04  Zhentiw  阅读(81)  评论(0编辑  收藏  举报