👀switch表达式
在JDK12中,switch表达式可以通过函数表达式方式省略break关键字, JDK13中又对switch
表达式进行了增强,增加了yield
关键词用于返回值,相比break
,语义更加明确了。
java
enum DayOfWeek {
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY,
SUNDAY
}
public static String test(DayOfWeek dayOfWeek) {
return
switch (dayOfWeek) {
case MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY: {
yield "工作日";
}
case SATURDAY, SUNDAY: {
yield "周末";
}
};
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21