黑马程序员技术交流社区
标题:
JAVA中的多重继承
[打印本页]
作者:
Synaric
时间:
2015-9-30 09:01
标题:
JAVA中的多重继承
interface I1{
void f();
}
interface I2{
int f(int i);
}
interface I3{
int f();
}
class C{
public int f(){
return 1;
}
}
class C2 implements I1,I2{
public void f(){ }
public int f(int i){
return 2;
}
}
class C3 extends C implements I2{
@Override
public int f(int i) {
return 3;
}
}
class C4 extends C implements I3{
public int f(){
return 4;
}
}
//The return types are incompatible for the inherited methods I1.f(), C.f()
//class C5 extends C implements I1{ }
//The return types are incompatible for the inherited methods I1.f(), I3.f()
//interface I4 extends I1, I3{ }
复制代码
就结果而言,子类会从继承和实现的方法中取得一个交集,交集中必须避免“方法签名相同但返回值不同”的情况。
作者:
ln0491
时间:
2015-9-30 09:20
不错,学习了。。。。
作者:
只求不悔
时间:
2015-9-30 09:46
继承中如果有很多方法重复了,就要一直复写的话,会不会有问题
作者:
Synaric
时间:
2015-9-30 09:57
只求不悔 发表于 2015-9-30 09:46
继承中如果有很多方法重复了,就要一直复写的话,会不会有问题
不会,同方法签名、同返回类型的方法只需要写一次,若出现同方法签名、不同返回类型直接报错
作者:
Synaric
时间:
2015-9-30 09:58
讲错了并集……
作者:
Arron
时间:
2015-10-4 18:23
好的 谢谢了.
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2