Is it possible to have two methods with the same name but different parameters and return types in Java? Seems like it would be a good way to generalize a simple getter and setter.. You can do that with constructors why not with regular methods ? for example
why not be able to do ..
int getVal() {
return int;
}
boolean getVal() {
return true;
}
setVal(int a) {
}
and
setVal(boolean a) {
}
解决方案
Because then the compiler would be unable to figure out:
setVal(getVal());
should it call the bool or int version?
博客探讨了Java中是否可以有同名但参数和返回类型不同的方法,认为这或许是通用化简单getter和setter的好方式,还给出示例代码。但解决方案指出,若如此编译器将无法确定方法调用,如setVal(getVal())该调用哪个版本。
6482

被折叠的 条评论
为什么被折叠?



