标签:开始 expr nta return stat poi try github 条件
本章主要介绍高级条件语句中的 switch 语句以及其增强版的模式匹配。
特点
break
才会停止执行。例:
int i = 5 + 5;
switch (i) {
case 1:
System.out.println("i is 1");
case 10:
System.out.println("i is 10");
case 100:
System.out.println("i is 100");
case 1000:
System.out.println("i is 1000");
break;
case 10000:
System.out.println("i is 10000");
default:
System.out.println("default");
}
以上语句的输出结果为
i is 10
i is 100
i is 1000
Groovy 的 switch 语句类似 Java,但是 Groovy 除了支持基本类型,也支持引用类型,包含,正则匹配等各种操作。
例:
static def testSwitch(var) {
def result
switch (var) {
case 0:
result = "Object equals"
break
case 11..20:
result = "Range contains"
break
case [1, 2, 3]:
result = "List contains"
break
case Float:
result = "Class instance"
break
case { it % 3 == 0 }:
result = "Closure"
break
case ~‘[0-9]{3}‘:
result = "Pattern match"
break
default:
result = "Default"
break
}
result
}
assert "Object equals" == testSwitch(0)
assert "Range contains" == testSwitch(15)
assert "List contains" == testSwitch(3)
assert "Class instance" == testSwitch(4.5f)
assert "Closure" == testSwitch(21)
assert "Pattern match" == testSwitch(910)
以上语句的输出结果为
i is 10
i is 100
i is 1000
Scala 没有 switch 语句,但是有类似功能的模式匹配。模式匹配的语法在外观上有些像 Java 的 switch 语句,但是没有 break 语句,在功能上则比 Java 的 switch 语句要强大很多。
例:
def matchObject(foo: Any): String = {
val result = foo match {
case 0 => "Object equals"
case i if i == 10 || i == 11 => "Expression"
case i: Int => s"Class instance holds $i"
case List(1, 2, _*) => "Match the last element of a list"
case Number(n) => s"Case class holds $n"
case t: {def length: Int} => "Class structure contains method length"
case _ => "Default"
}
result
}
println(matchObject(0)) // Object equals
println(matchObject(10)) // Expression
println(matchObject(20)) // Class instance holds 20
println(matchObject(List(1, 2, 1000, 2000))) // Match the last element of a list
println(matchObject(Number(1))) // Case class holds 1
println(matchObject(List(3, 2, 1000, 2000))) // Method contains
除了以上示例,模式匹配还可以用在 try...catch
语句中
def matchTryCatch: Unit = {
try {
throw new NullPointerException
} catch {
case e: NullPointerException => println("NullPointerException")
case e: IndexOutOfBoundsException => print("IndexOutOfBoundsException")
} finally {
println("finally")
}
}
Kotlin 也没有 switch 语句,但是有功能近似的 when 语句,并且使用 when 语句也不需使用 break 语句。
例:
private fun test(foo: Any): String {
var result = when (foo) {
0 -> "Object equals"
3, 10 -> "Or"
in 11..20 -> "Range contains"
is Date -> "Class instance"
!in 4..30 -> "Range not contain"
else -> "Default"
}
return result
}
assert("Object equals" == test(0))
assert("Or" == test(3))
assert("Range contains" == test(11))
assert("Range not contain" == test(33))
assert("Class instance" == test(Date()))
除了以上示例,when 还可以用来替代 if-else
语句
val x = 10
val y = 20
when {
x + y == 5 -> println("x + y = 5")
x + y == 10 -> println("x + y = 10")
else -> println("x + y != 5 or 10")
}
文章源码见 https://github.com/SidneyXu/JGSK仓库的 _20_switch_match
小节
Java & Groovy & Scala & Kotlin - 20.Switch 与模式匹配
标签:开始 expr nta return stat poi try github 条件
原文地址:https://www.cnblogs.com/feng9exe/p/10458032.html