java支持切換一個字符串,但是在case字段中,如何使用一個常量字符串? 像這樣:爲什麼public static final string不能用在java開關
PhxLog l = new PhxLog();
for (String s : l.FIELDS) {
Object v = mapBaseData.getData().get(s);
switch (s) {
case l.LOG_FIELD_TIME: // this is wrong, but how to use a constant instead of a "xxxx"
l.setTime((String)v);
break;
請顯示'l'的定義,它是類和'LOG_FIELD_TIME'的值。如果你真的想在這裏切換,爲什麼不枚舉? –
爲什麼你的代碼不能正常工作?似乎在這裏工作。 http://ideone.com/tiS3RA –
我做了一個快速測試。如果'l'是對'ClassOfL'的引用,並且''ClassOfL''中的'LOG_FIELD_TIME'被定義爲'private static final String',那麼'case ClassOfL.LOG_FIELD_TIME:..'應該可以工作。 –