【发布时间】:2013-08-11 01:08:44
【问题描述】:
public static void main(String args[]) {
myMethod(); // i am calling static method from main()
}
.
public static ? myMethod(){ // ? = what should be the return type
return value;// is String
return index;// is int
}
myMethod() 将返回 String 和 int 值。因此,从main() 获取这些返回值我想出了以下解决方案。
创建一个类调用ReturningValues
public class ReturningValues {
private String value;
private int index;
// getters and setters here
}
并如下更改myMethod()。
public static ReturningValues myMethod() {
ReturningValues rv = new ReturningValues();
rv.setValue("value");
rv.setIndex(12);
return rv;
}
现在我的问题是,有没有更简单的方法来实现这一点?
【问题讨论】:
-
您可以使用
Properties或HashMap甚至List,但我认为您的ReturnValues更合适,因为它对于该方法将返回什么是明确的 -
索引和值有什么关系?
标签: java return return-type