JavaSwitch語句:常量表達式是必需的,但它是常量所以,我正在研究這個類,它有幾個靜態常量:public abstract class Foo {
...
public static final int BAR;
public static final int BAZ;
public static final int BAM;
...}然后,我想要一種基于常量獲得相關字符串的方法:public static String lookup(int constant) {
switch (constant) {
case Foo.BAR: return "bar";
case Foo.BAZ: return "baz";
case Foo.BAM: return "bam";
default: return "unknown";
}}但是,當我編譯時,我會得到一個constant expression required三個大小寫標簽上的每個錯誤。我知道編譯器需要在編譯時知道表達式才能編譯一個開關,但為什么不知道呢?Foo.BA_常量?
JavaSwitch語句:常量表達式是必需的,但它是常量
一只名叫tom的貓
2019-07-27 15:15:35