我是一名涉足 Groovy 的 Java 程序员。您会在我的代码中注意到,我混合了一些特定于 Java 的语法,这在 Groovy 中被认为是 A-Okay。
谁能向我解释为什么 Groovy 不接受静态变量作为CASE
参数?或者如果可以,你能看到我在这里做错了什么吗?
public static final String HIGH_STRING = "high";
public static final String LOW_STRING = "low";
... //other code, method signature, etc.
def val = "high";
switch (val) {
case HIGH_STRING:
println("string was high"); //this won't match
break;
case LOW_STRING:
println("string was low"); //this won't match
break;
//case "high":
// println("string was high"); //this will match because "high" is a literal
// break;
default:
println("no match");
}
... //other code, method closeout, etc.