黑马程序员技术交流社区

标题: JAVA中的多重继承 [打印本页]

作者: Synaric    时间: 2015-9-30 09:01
标题: JAVA中的多重继承
  1. interface I1{
  2.         void f();
  3. }

  4. interface I2{
  5.         int f(int i);
  6. }

  7. interface I3{
  8.         int f();
  9. }

  10. class C{
  11.         public int f(){
  12.                 return 1;
  13.         }
  14. }

  15. class C2 implements I1,I2{
  16.         public void f(){ }
  17.         public int f(int i){
  18.                 return 2;
  19.         }
  20. }

  21. class C3 extends C implements I2{
  22.         @Override
  23.         public int f(int i) {
  24.                 return 3;
  25.         }
  26.        
  27. }

  28. class C4 extends C implements I3{
  29.         public int f(){
  30.                 return 4;
  31.         }
  32. }

  33. //The return types are incompatible for the inherited methods I1.f(), C.f()
  34. //class C5 extends C implements I1{        }

  35. //The return types are incompatible for the inherited methods I1.f(), I3.f()
  36. //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