我使用if
和else if
來查找某種類型的代碼並從中創建相應的值。我不知道如何使它更有效,我發現在一個論壇下面的文章,但我沒有像類型boolean
,我的類型爲bollean.edm
,char.edm
等在不同的枚舉類型中對Java 1.6中的字符串使用switch開關
是否有使用下面的代碼的方式調整以支持我的情況?
public static void main(String[] args) throws InterruptedException {
String typeName = "Boolean";
String memberValue = "memberValue";
SwitchInputType type = Type.valueOf(typeName).makeType(memberValue);
}
enum Type {
Boolean {
SwitchInputType makeType(String memberValue) {
return new SwitchInputType<Boolean>(new Boolean(memberValue));
}
},
Double {
SwitchInputType makeType(String memberValue) {
return new SwitchInputType<Double>(new Double(memberValue));
}
},
Int32 {
SwitchInputType makeType(String memberValue) {
return new SwitchInputType<Integer>(new Integer(memberValue));
}
};
// All must do this.
abstract SwitchInputType makeType(String memberValue);
}
static class SwitchInputType<T> {
public SwitchInputType(Object o) {
}
}
你能否試着更準確地解釋你的目標是什麼?有什麼知道你的類型'bollean.edm'等? – user905686 2013-03-23 10:12:57
我不認爲這是類型應提供爲字符串的問題,順便說一句,這是Odata類型 – 2013-03-23 10:16:45