class EnumKotlin {
enum class ProtocolState {
WAITING {
override fun fetchAlias() = "my alias is waiting"
},
TALKING {
override fun fetchAlias() = "my alias is talking"
override fun fetchId() = 1;
};
abstract fun fetchAlias(): String
open fun fetchId(): Int {
return 0;
}
}
enum class Color(val rgb: Int) {
RED(0xFF0000),
GREEN(0x00FF00),
BLUE(0x0000FF)
}
enum class Direction {
NORTH, SOUTH, WEST, EAST
}
}
枚举类必须放在已经定义的类里面 或自己直接声明为一个kotlin枚举类, 且不能定义在 方法里面 , 如果kotlin文件没有定义类名,直接放到里面也是不行的除非你就把它作为枚举类
var value = EnumKotlin.Color.BLUE.rgb
println("enum declared order:$ordinal enum value :${Integer.toHexString(value)}")
println("enum protocol name ${alias} ID:${EnumKotlin.ProtocolState.TALKING.fetchId()}")
测试结果:
enum declared order:2 enum value :ff
enum protocol name my alias is talking ID:1