一个小程序,我编码的一部分:的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 –