kotlin中when的用法
when是什么
when用于在kotlin中实现类c语言中的switch关键字
when怎么用
when (x) {
1 -> print("x == 1")
2 -> print("x == 2")
else -> { // Note the block
print("x is neither 1 nor 2")
}
}
差不多是类C语言中这么个意思
swicth(x){
case 1:
.......
break;
case 2:
.......
break;
default: // kotlin else
.......
break;
}
帮助
https://kotlinlang.org/docs/reference/control-flow.html