码迷,mamicode.com
首页 > 编程语言 > 详细

Java实现多重继承:

时间:2015-10-23 14:49:13      阅读:327      评论:0      收藏:0      [点我收藏+]

标签:

 

Java没有多重继承,C++有,不过Java提供了Interface、extends和implement,多重继承的效果还是可以的:

 

  1 /**
  2  * Created by Franklin Yang on 2015.10.23.
  3  */
  4 
  5 //
  6 interface Base {
  7     public void method1();
  8 }
  9 
 10 interface aI extends Base{
 11     public void method2();
 12     public void method3();
 13 //    public void method4();
 14 //    public void method5();
 15 }
 16 
 17 interface aII extends aI {
 18 //    public void method1();
 19 //    public void method2();
 20 //    public void method3();
 21     public void method4();
 22     public void method5();
 23 }
 24 
 25 class aBase implements Base{
 26     public void method1() {
 27         System.out.println("类aBase实现接口Base的方法1");
 28     }
 29 }
 30 
 31 class aB extends aBase implements aI{
 32 //    public void method1() {
 33 //        System.out.println("类aB实现接口aI的方法1");
 34 //    }
 35     public void method2() {
 36         System.out.println("类aB实现接口aI的方法2");
 37     }
 38     public void method3() {
 39         System.out.println("类aB实现接口aI的方法3");
 40     }
 41  42  43 //    public void method4() {}
 44 //    public void method5() {}
 45 }
 46 
 47 class aD extends aB implements aII{
 48 //    public void method1() {
 49 //        System.out.println("类aD实现接口aI的方法1");
 50 //    }
 51  52  53 //    public void method2() {}
 54 //    public void method3() {}
 55     public void method4() {
 56         System.out.println("类aD实现接口aII的方法4");
 57     }
 58     public void method5() {
 59         System.out.println("类aD实现接口aII的方法5");
 60     }
 61 }
 62 
 63 //  added by whyang
 64 class iB{
 65     public void depend1(Base b){
 66         b.method1();
 67     }
 68 }
 69 
 70 
 71 class aA{
 72     public void depend1(aI i){
 73         i.method1();
 74     }
 75     public void depend2(aI i){
 76         i.method2();
 77     }
 78     public void depend3(aI i){
 79         i.method3();
 80     }
 81 }
 82 
 83 class aC{
 84     public void depend1(aII i){
 85         i.method1();
 86     }
 87     public void depend2(aII i){
 88         i.method2();
 89     }
 90     public void depend3(aII i){
 91         i.method3();
 92     }
 93     public void depend4(aII i){
 94         i.method4();
 95     }
 96     public void depend5(aII i){
 97         i.method5();
 98     }
 99 }
100 
101 
102 public class Isolate2{
103     public static void main(String[] args){
104         System.out.println("11111111");
105         iB base = new iB();
106         base.depend1(new aB());
107 
108         System.out.println("22222222");
109         aA a = new aA();
110         a.depend1(new aB());
111         a.depend2(new aB());
112         a.depend3(new aB());
113 
114         System.out.println("33333333");
115         aC c = new aC();
116         c.depend1(new aD());
117         c.depend2(new aD());
118         c.depend3(new aD());
119         c.depend4(new aD());
120         c.depend5(new aD());
121     }
122 }

 

跑一跑:

11111111
类aBase实现接口Base的方法1
22222222
类aBase实现接口Base的方法1
类aB实现接口aI的方法2
类aB实现接口aI的方法3
33333333
类aBase实现接口Base的方法1
类aB实现接口aI的方法2
类aB实现接口aI的方法3
类aD实现接口aII的方法4
类aD实现接口aII的方法5

Process finished with exit code 0

 

Java实现多重继承:

标签:

原文地址:http://www.cnblogs.com/andypeker/p/4904471.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!