1.内容穿透:
-
import java.lang.*;
-
-
/*
-
*1.内容穿透,只要case的内容有包含参数字段,就会执行
-
*/
-
public
class demo1{
-
public
static void main(
String [] args){
-
String
A=
"a";
-
switch(
A){
-
case
"a":
-
System.out.
println(
"打印a");
-
case
"ab":
-
System.out.
println(
"打印ab");
-
case
"abc":
-
System.out.
println(
"打印abc");
-
}
-
System.out.
println(
"结束");
-
-
}
-
-
}
打印a 打印ad 打印abc 结束
-
/*
-
*2.在没有遇到break都会继续往下执行
-
*/
-
public static void main(String[] args) {
-
int num =
2;
-
switch (num) {
-
case
1:
-
++num;
-
case
2:
-
++num;
-
case
3:
-
++num;
-
default:
-
++num;
-
break;
-
}
-
System.
out.println(num);
-
}
-
}
匹配到case 2 num->3 但是其后没有break,所以继续执行case 3 num->4、default num->5而后结束,所以答案为 5