The return types are incompatible for the * inherited methods I.f(), J.f()
package c12_Exception;
interface I {
// 假设:早期接口,已经被很多类实现,不宜修改该方法名。
void f();
String s();
}
abstract class J {
// 假设:早期抽象类,已经被很多类继承,不宜修改该方法名。
int f() {
return 1;
}
public String s() {
return null;
}
}
/*
* 现在有一个需要继承抽象类 J 并实现 接口 I,如何规避(The return types are incompatible for the
* inherited methods I.f(), J.f()),而不修改方法名。
*/
public class TestAbstract extends J implements I {
}
如题,就是因为继承J实现I后,J中的f()方法与I中的f()方法同名,方法签名也一样,发生了重载,但俩方法的返回值不一样(假设,实际上这2方法本来就没任何关系,只是早期设计时同名了而已),在不修改方法的情况下,怎么解决? inherit abstract interface --------------------编程问答-------------------- 这个不能解决,因为重载的定义就是参数类型与个数可以不同,编译器才好区分该调用哪个重载的函数,编译器不会根据返回类型来区分重载函数 --------------------编程问答-------------------- #1你好,这个问题是因为继承后发生重载造成的,只是不清楚该如何解决这个问题,有没有那种方法或设计模式可以解决呢? --------------------编程问答-------------------- 我想到了一种方法。。。
package c12_Exception;
interface I {
// 假设:早期接口,已经被很多类实现,不宜修改该方法名。
void f();
String s();
}
abstract class J {
// 假设:早期抽象类,已经被很多类继承,不宜修改该方法名。
int f() {
return 1;
}
public String s() {
return null;
}
}
/*
* 现在有一个需要继承抽象类 J 并实现 接口 I,如何规避(The return types are incompatible for the
* inherited methods I.f(), J.f()),而不修改方法名。
*/
public class TestAbstract implements I {
private JJ jj = new JJ();
class JJ extends J {
}
// 实现接口中的f()方法
public void f() {
}
// 得到抽象类的f()方法
public int getF() {
return jj.f();
}
public String s() {
return jj.s();
}
}
补充:Java , Java相关