java继承和组合

要实现的目标:鸟(Bird)和狼(Wolf)都是动物(Animal),动物都有心跳(beat()),会呼吸(beat()),但是鸟会fly(fly()),狼会奔跑(run()),用java程序实现以上描述。

InheritTest.java 使用继承方式实现目标

CompositeTest.java 使用组合方式实现目标

  1. //InheritTest.java 使用继承方式实现目标  
  2. class Animal{  
  3.     private void beat(){  
  4.         System.out.println("心脏跳动...");  
  5.     }  
  6.     public void breath(){  
  7.         beat();  
  8.         System.out.println("吸一口气,呼一口气,呼吸中...");  
  9.     }  
  10. }  
  11. //继承Animal,直接复用父类的breath()方法  
  12. class Bird extends Animal{  
  13.     //创建子类独有的方法fly()  
  14.     public void fly(){  
  15.         System.out.println("我是鸟,我在天空中自由的飞翔...");  
  16.     }  
  17. }  
  18. //继承Animal,直接复用父类的breath()方法  
  19. class Wolf extends Animal{  
  20.     //创建子类独有的方法run()  
  21.     public void run(){  
  22.         System.out.println("我是狼,我在草原上快速奔跑...");  
  23.     }  
  24. }  
  25. public class InheritTest{  
  26.     public static void main(String[] args){  
  27.         //创建继承自Animal的Bird对象新实例b  
  28.         Bird b=new Bird();  
  29.         //新对象实例b可以breath()  
  30.         b.breath();  
  31.         //新对象实例b可以fly()  
  32.         b.fly();  
  33.         Wolf w=new Wolf();  
  34.         w.breath();  
  35.         w.run();  
  36. /* 
  37. ---------- 运行Java程序 ---------- 
  38. 心脏跳动... 
  39. 吸一口气,呼一口气,呼吸中... 
  40. 我是鸟,我在天空中自由的飞翔... 
  41. 心脏跳动... 
  42. 吸一口气,呼一口气,呼吸中... 
  43. 我是狼,我在草原上快速奔跑... 
  44.  
  45. 输出完毕 (耗时 0 秒) - 正常终止 
  46. */  
  47.     }  
  48. }  
  49.   
  50. //CompositeTest.java  使用组合方式实现目标  
  51. class Animal{  
  52.     private void beat(){  
  53.         System.out.println("心脏跳动...");  
  54.     }  
  55.     public void breath(){  
  56.         beat();  
  57.         System.out.println("吸一口气,呼一口气,呼吸中...");  
  58.     }  
  59. }  
  60. class Bird{  
  61.     //定义一个Animal成员变量,以供组合之用  
  62.     private Animal a;  
  63.     //使用构造函数初始化成员变量  
  64.     public Bird(Animal a){  
  65.         this.a=a;  
  66.     }  
  67.     //通过调用成员变量的固有方法(a.breath())使新类具有相同的功能(breath())  
  68.     public void breath(){  
  69.         a.breath();  
  70.     }  
  71.     //为新类增加新的方法  
  72.     public void fly(){  
  73.         System.out.println("我是鸟,我在天空中自由的飞翔...");  
  74.     }  
  75. }  
  76. class Wolf{  
  77.     private Animal a;  
  78.     public Wolf(Animal a){  
  79.         this.a=a;  
  80.     }  
  81.     public void breath(){  
  82.         a.breath();  
  83.     }  
  84.     public void run(){  
  85.         System.out.println("我是狼,我在草原上快速奔跑...");       
  86.     }  
  87. }  
  88. public class CompositeTest{  
  89.     public static void main(String[] args){  
  90.         //显式创建被组合的对象实例a1  
  91.         Animal a1=new Animal();  
  92.         //以a1为基础组合出新对象实例b  
  93.         Bird b=new Bird(a1);  
  94.         //新对象实例b可以breath()  
  95.         b.breath();  
  96.         //新对象实例b可以fly()  
  97.         b.fly();  
  98.         Animal a2=new Animal();  
  99.         Wolf w=new Wolf(a2);  
  100.         w.breath();  
  101.         w.run();  
  102. /* 
  103. ---------- 运行Java程序 ---------- 
  104. 心脏跳动... 
  105. 吸一口气,呼一口气,呼吸中... 
  106. 我是鸟,我在天空中自由的飞翔... 
  107. 心脏跳动... 
  108. 吸一口气,呼一口气,呼吸中... 
  109. 我是狼,我在草原上快速奔跑... 
  110.  
  111. 输出完毕 (耗时 0 秒) - 正常终止 
  112. */  
  113.     }  
  114. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值