A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 庄星睿 中级黑马   /  2012-6-11 17:29  /  2025 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

先用继承实现以下 Animal  ,Bird ,Wolf之间的关系
  1. class Animal
  2. {
  3. private void beat()
  4. {
  5. System.out.println("心脏跳动.........");
  6. }
  7. public void breath()
  8. {
  9. beat();
  10. System.out.println("呼吸.......");
  11. }

  12. }

  13. class Bird extends Animal
  14. {
  15. public void fly()
  16. {
  17. System.out.println("flying.......");
  18. }

  19. }

  20. class Wolf extends Animal
  21. {
  22. public void run()
  23. {
  24. System.out.println("running.......");

  25. }

  26. }

  27. public class InheritTest
  28. {
  29. public static void main(String[] args)
  30. {
  31. Bird b=new Bird();
  32. b.breath();
  33. b.fly();

  34. Wolf w=new Wolf();
  35. w.breath();
  36. w.run();

  37. }

  38. }
复制代码
先不考虑别的,如果用组合的方式实现代码复用:
  1. class Animal
  2. {
  3. private void beat()
  4. {
  5. System.out.println("心脏跳动........");
  6. }
  7. public void breath()
  8. {
  9. beat();
  10. System.out.println("呼吸........");
  11. }
  12. }


  13. class Bird   
  14. {
  15. private Animal a;
  16. public Bird(Animal a)
  17. {
  18. this.a=a;
  19. }
  20. public void breath()
  21. {
  22. a.breath();
  23. }
  24. public void fly()
  25. {
  26. System.out.println("flying.......");
  27. }
  28. }

  29. class Wolf
  30. {
  31. private Animal a;
  32. public Wolf(Animal a)
  33. {
  34. this.a=a;
  35. }
  36. public void breath()
  37. {
  38. a.breath();
  39. }

  40. public void run()
  41. {
  42. System.out.println("running.......");

  43. }

  44. }

  45. public class CompositeTest
  46. {
  47. public static void main(String[] args)
  48. {
  49. Animal a1=new Animal();
  50. Bird b=new Bird(a1);
  51. b.breath();
  52. b.fly();

  53. Animal a2=new Animal();
  54. Wolf w=new Wolf(a2);
  55. w.breath();
  56. w.run();


  57. }

  58. }
复制代码
代码比较简单,但能表达组合就是将原来父类Animal嵌入进子类中,从而成为了子类中的一部分,就是"has a"关系,在内存空间上,组合比继承多一个在栈内存中的引用那个父类对象的引用变量,当然这里还是用继承好一些,那个关于聚合的例子谁给一个,最好能简单说明问题滴,视频里只是说了一下聚合和组合的紧密程度不同,那聚合的实现方式也应该和组合一样吧,谁给一个详细实现方式的例子

6 个回复

正序浏览
赵兵锋 发表于 2012-6-11 20:48
额,我也不知了,请其他兄弟来看吧。
顺便问一下,你看的什么书?哪一页? ...

java 讲义 精粹版的
第四章 4.8.2 利用组合实现复用 那一节
回复 使用道具 举报
庄星睿 发表于 2012-6-11 20:45
老大,他们的关系我知道,就是用代码怎样实现出来啊,我那个例子是在书上看的,他也描述了,但没描述聚合 ...

额,我也不知了,请其他兄弟来看吧。
顺便问一下,你看的什么书?哪一页?
回复 使用道具 举报
赵兵锋 发表于 2012-6-11 19:59
聚合关系是“has-a”关系,组合关系是“contains-a”关系;聚合关系表示整体与部分的关系比较弱,而组合比 ...

老大,他们的关系我知道,就是用代码怎样实现出来啊,我那个例子是在书上看的,他也描述了,但没描述聚合,而且代码注明的是用组合实现复用,我就想问他既然组合是这么用的,那聚合该怎么用啊,悲催的书上没写,而你给的那个代码,聚合用的和我写的一样,感觉正好颠倒了,你和书上到底哪个正确啊
回复 使用道具 举报
聚合关系是“has-a”关系,组合关系是“contains-a”关系;聚合关系表示整体与部分的关系比较弱,而组合比较强;聚合关系中代表部分事物的对象与代表聚合事物的对象的生存期无关,一旦删除了聚合对象不一定就删除了代表部分事物的对象。组合中一旦删除了组合对象,同时也就删除了代表部分事物的对象。
可以这样通俗理解:
你和你女朋友是聚合关系,是"has-a"
你和你的身体的组合关系,是"contains-a"
你要是死了,你身体肯定也没用了,一损俱损,但是你的女朋友不一定,她还可以跟别人搞聚合关系去
回复 使用道具 举报
赵兵锋 发表于 2012-6-11 18:22

哥们,你写的聚合怎么和我写的组合是一样的啊,我看的书上写的那种方式是利用组合实现的啊,但书上没介绍聚合,如果按你写的那样  this.animal=new Animal()  他依赖Animal类更紧密些了,难道是书上写错了,啊啊啊,求解啊......
回复 使用道具 举报
  1. public class Main {
  2.         public static void main(String[] args) throws Exception {
  3.                 Animal a = new Animal();
  4.                 Cat c = new Cat(a);//将创建的Animal对象传递给Cat对象,此为聚合
  5.         }
  6. }
  7. class Animal{}
  8. class Cat{
  9.         private Animal animal;
  10.         public Cat(Animal animal){//此种方式为聚合,Animal对象并不由Cat创建,由外部传递引用,这样就避免了当Cat对象被销毁时,其中的Animal对象也会被销毁。
  11.                 this.animal = animal;
  12.         }
  13.         public Cat(){
  14.                 this.animal = new Animal();//此种方式为组合,Cat对象销毁时,其中的Animal对象也会被销毁。
  15.         }
  16. }
复制代码
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马