考虑一种产生不同类型结果的方法.在我的例子中,它是ArrayList或Integer(伪代码):
int a = ... // value for a comes from another function
public ArrayList compute(){ // return either ArrayList or Integer
if(a==1){
ArrayList result = new Arraylist()
for(int i=0; i<=something; i++){
arr.add(...);
}
}
if(a==2){
int result;
result = somethingElse;
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
根据结果a,结果result来自循环并将结果加载到ArrayList中,或者在第二种情况下,它只是一个数字.
该方法应返回什么类型?
| 归档时间: |
|
| 查看次数: |
50 次 |
| 最近记录: |