对于方法重载的区分,主要通过下面三种方式:
1. 参数个数
2. 参数类型
3. 参数顺序(较少使用,维护困难)
至于方法的其他部分,如方法返回值类型、修饰符等,与方法重载则没有任何关系。、
Java编程时,假设存在两个接口,但接口中存在相同名称的方法,但是其仅返回值不同。如下:
interface interfac1{
void method();
}
interface interface2 {
int method();
}
interface interface3 extends interfac1,interface2{
}
这段程序看上去是合理的,但是程序实则是错误的。这就涉及到方法的重载问题,这里仅用返回值作为区分是无法进行方法重载的,所以这两个接口中的method()方法,会被当做相同的方法。但是其返回值不同,又造成了矛盾。所以,程序会报错The return types are incompatible for the inherited methods interfac1.f(), interface2.f()。
所以在Java中,当方法名称相同,仅仅根据方法返回值来区分重载方法是行不通的。