Kotlin 简明教程
Kotlin - When Expression
考虑一下当你需要检查大量条件的情况。虽然你可以使用 if..else if 表达式来处理这种情况,但Kotlin提供了 when 表达式以更友善的方式来处理这种情况。与编写许多 if…else if 表达式相比,使用 when 表达式更加简单和干净。如以下示例所示,Kotlin when 表达式在许多备选方案中估算一段代码。
Consider a situation when you have large number of conditions to check. Though you can use if..else if expression to handle the situation, but Kotlin provides when expression to handle the situation in nicer way. Using when expression is far easy and more clean in comparison to writing many if…else if expressions. Kotlin when expression evaluates a section of code among many alternatives as explained in below example.
Kotlin when 按顺序依次检查其参数与所有分支,直到满足某个分支条件。
Kotlin when matches its argument against all branches sequentially until some branch condition is satisfied.
Example
fun main(args: Array<String>) {
val day = 2
val result = when (day) {
1 -> "Monday"
2 -> "Tuesday"
3 -> "Wednesday"
4 -> "Thursday"
5 -> "Friday"
6 -> "Saturday"
7 -> "Sunday"
else -> "Invalid day."
}
println(result)
}
当你运行上述 Kotlin 程序时,它将生成以下输出:
When you run the above Kotlin program, it will generate the following output:
Tuesday
Kotlin when as Statement
Kotlin when 既可以用作表达式,也可以用作语句,就像Java中的switch语句一样。如果它用作表达式,则首个匹配分支的值将变为整体表达式的值。
Kotlin when can be used either as an expression or as a statement, simply like a switch statement in Java. If it is used as an expression, the value of the first matching branch becomes the value of the overall expression.
Example
让我们再次编写上面的示例而不使用表达式形式:
Let’s write above example once again without using expression form:
fun main(args: Array<String>) {
val day = 2
when (day) {
1 -> println("Monday")
2 -> println("Tuesday")
3 -> println("Wednesday")
4 -> println("Thursday")
5 -> println("Friday")
6 -> println("Saturday")
7 -> println("Sunday")
else -> println("Invalid day.")
}
}
当你运行上述 Kotlin 程序时,它将生成以下输出:
When you run the above Kotlin program, it will generate the following output:
Tuesday
Combine when Conditions
我们可以将多个 when 条件组合成单个条件。
We can combine multiple when conditions into a single condition.
Range in when Conditions
Kotlin范围使用双点 .. 创建,并且我们可以使用 in 运算符在检查 when 条件时使用它们。
Kotlin ranges are created using double dots .. and we can use them while checking when condition with the help of in operator.
Expression in when Conditions
Kotlin when 可以使用任意表达式代替常量作为分支条件。
Kotlin when can use arbitrary expressions instead of a constant as branch condition.
Kotlin when with block of code
Kotlin when 分支可以作为用花括号括起来的代码块放置。
Kotlin when braches can be put as block of code enclosed within curly braces.
Example
fun main(args: Array<String>) {
val day = 2
when (day) {
1 -> {
println("First day of the week")
println("Monday")
}
2 -> {
println("Second day of the week")
println("Tuesday")
}
3 -> {
println("Third day of the week")
println("Wednesday")
}
4 -> println("Thursday")
5 -> println("Friday")
6 -> println("Saturday")
7 -> println("Sunday")
else -> println("Invalid day.")
}
}
当你运行上述 Kotlin 程序时,它将生成以下输出:
When you run the above Kotlin program, it will generate the following output:
Second day of the week
Tuesday