我正在学习GoF Java设计模式,我想看看它们的一些真实例子.Java核心库中这些设计模式的一些很好的例子是什么?
为什么仅通过更改返回类型来重载函数是不可能的?这将在未来的Java版本中发生变化吗?
那么,仅供参考,这在C++中是否可行?
public static void main(String args[]) {
myMethod(); // i am calling static method from main()
}
Run Code Online (Sandbox Code Playgroud)
.
public static ? myMethod(){ // ? = what should be the return type
return value;// is String
return index;// is int
}
Run Code Online (Sandbox Code Playgroud)
myMethod()将返回String和int值.所以从main()我拿出这些返回值得出以下解决方案.
创建一个类调用 ReturningValues
public class ReturningValues {
private String value;
private int index;
// getters and setters here
}
Run Code Online (Sandbox Code Playgroud)
并改变myMethod()如下.
public static ReturningValues myMethod() {
ReturningValues rv = new ReturningValues();
rv.setValue("value");
rv.setIndex(12);
return rv;
}
Run Code Online (Sandbox Code Playgroud)
现在我的问题是,有没有更简单的方法来实现这一点?