public class Switch extends Stat implements CaseSetter
示例:
#switch (month)
#case (1, 3, 5, 7, 8, 10, 12)
#(month) 月有 31 天
#case (2)
#(month) 月平年有28天,闰年有29天
#default
月份错误: #(month ?? "null")
#end
如上代码所示,如果 #case 指令参数有多个值,那么可以用逗号分隔,
上述逗号表达式的值 1, 3, 5, 7, 8, 10, 12 之中只要有一个与
switch 指令参数 month 相等的话,该 case 分支就会被执行,
该特性从根本上消灭了 #break 指令的必要性
除了常量值以外 #case 参数还可以是任意表达式
例如:
#case (a, b, c, x + y, obj.method(z))
上述代码中 #case 参数中的所有表达式先会被求值,然后逐一与 #switch
参数进行对比,同样也是只要有一个对比相等,则该 case 分支就会被执行
| 限定符和类型 | 方法和说明 |
|---|---|
void |
exec(Env env,
Scope scope,
Writer writer) |
boolean |
hasEnd() |
void |
setDefault(Default _default,
Location location) |
void |
setNextCase(Case nextCase) |
getLocation, setExprList, setLocation, setStat, writeCopyright © 2024. All rights reserved.