一個小程序,我編碼的一部分:的Java:方法返回String或詮釋
String maxs = "";
int maxi = 0;
在這一部分,我定義了兩個變量int
和String
。
public Class(String max){
try {
this.maxi = Integer.parseInt(max);
}catch (Exception e){
this.maxs = max;
}
}
我想這樣我會去定義這兩個變量之一,如果字符串不解析爲int它將被保存爲正常的字符串。
現在,我需要檢查什麼,我需要返回:
private TypeOfReturn getMax(){
if(this.maxi == 0){
// return maxs (return String)
} else if (this.maxs.equals("")) {
// return maxi (return int)
} else return null;
}
的quastion是,我怎麼補法getMax()
的缺失部分?
在Java中它甚至可以使用嗎?
TypeOfReturn可以是'Object'。 –
this.maxi = Integer.parseInt(max);最大來自哪裏? – Omore
檢查這個http://stackoverflow.com/questions/2127318/java-how-can-i-do-dynamic-casting-of-a-variable-from-one-type-to-another –