枚举
这个java中的枚举是类似的
初始化有两种方式,默认类型和指定类型
fun BaseMain() {
var state = State.DOING
println("the name is ${state.name} and the ordinal is ${state.ordinal} ")
var dir = Direction.SSS
}
enum class State {
START, DOING, PAUSE, STOP {
fun call() {
println("This is stop")
}
}
}
enum class Direction(val h: Boolean) {
ST(false), SSS(true)
}
利用名字实例化
fun BaseMain() {
var state = State.DOING
println("the name is ${state.name} and the ordinal is ${state.ordinal} ")
var dir = Direction.SSS
var dir2 = Direction.valueOf("SSS")
println("This is very good you like ${dir2.h} and orignal is ${dir2.ordinal}")
}
enum class State {
START, DOING, PAUSE, STOP
}
enum class Direction(val h: Boolean) {
ST(false), SSS(true)
}
这个用起来很方便的 不是吗
匿名类-定义方法
就是在枚举的内部定义一个抽象的方法,然后每一个枚举实例都需要实现它
enum class ProtocolState {
WAITING {
override fun call(): String {
return "LLLLL"
}
override fun signal() = TALKING
},
TALKING {
override fun call(): String {
return "Let us go for this moment."
}
override fun signal() = WAITING
};
abstract fun signal(): ProtocolState
abstract fun call(): String
}
fun BaseMain() {
var pro = ProtocolState.TALKING
println("The sigmal's name is ${pro.signal()} ")
println("Call the another function : ${pro.call()} ")
}
完结