疯狂JAVA讲义---第五章(下):面向对象

这章最后主要讲了复用:继承复用和组合复用,分别举了个例子来说明:

继承:eg

  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. //继承Animal,直接复用父类的breath方法
  14. class Bird extends Animal
  15. {
  16.     public void fly()
  17.     {
  18.         System.out.println("我在天空自在的飞翔...");
  19.     }
  20. }
  21. //继承Animal,直接复用父类的breath方法
  22. class Wolf extends Animal
  23. {
  24.     public void run()
  25.     {
  26.         System.out.println("我在陆地上的快速奔跑...");
  27.     }
  28. }
  29. public class TestInherit
  30. {
  31.     public static void main(String[] args)
  32.     {
  33.         Bird b = new Bird();
  34.         b.breath();
  35.         b.fly();
  36.         Wolf w = new Wolf();
  37.         w.breath();
  38.         w.run();        
  39.     }
  40. }

 

组合:

  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.     //将原来的父类嵌入原来的子类,作为子类的一个组合成分
  16.     private Animal a;
  17.     public Bird(Animal a)
  18.     {
  19.         this.a = a;
  20.     }
  21.     //重新定义一个自己的breath方法
  22.     public void breath()
  23.     {
  24.         //直接复用Animal提供的breath方法来实现Bird的breath方法。
  25.         a.breath();
  26.     }
  27.     public void fly()
  28.     {
  29.         System.out.println("我在天空自在的飞翔...");
  30.     }
  31. }
  32. class Wolf
  33. {
  34.     //将原来的父类嵌入原来的子类,作为子类的一个组合成分
  35.     private Animal a;
  36.     public Wolf(Animal a)
  37.     {
  38.         this.a = a;
  39.     }
  40.     //重新定义一个自己的breath方法
  41.     public void breath()
  42.     {
  43.         //直接复用Animal提供的breath方法来实现Bird的breath方法。
  44.         a.breath();
  45.     }
  46.     public void run()
  47.     {
  48.         System.out.println("我在陆地上的快速奔跑...");
  49.     }
  50. }
  51. public class TestComposite
  52. {
  53.     public static void main(String[] args)
  54.     {
  55.         //此时需要显式创建被嵌入的对象
  56.         Animal a1 = new Animal();
  57.         Bird b = new Bird(a1);
  58.         b.breath();
  59.         b.fly();
  60.         //此时需要显式创建被嵌入的对象
  61.         Animal a2 = new Animal();
  62.         Wolf w = new Wolf(a2);
  63.         w.breath();
  64.         w.run();        
  65.     }
  66. }

后面讲了初始化块,以前不怎么用,刚知道他还分静态初始化和普通初始化。这个没什么好说的看代码就知道了。eg

  1. class Root
  2. {
  3.     static{
  4.         System.out.println("Root的静态初始化块");
  5.     }
  6.     {
  7.         System.out.println("Root的普通初始化块");
  8.     }
  9.     public Root()
  10.     {
  11.         System.out.println("Root的无参数的构造器");
  12.     }
  13. }
  14. class Mid extends Root
  15. {
  16.     static{
  17.         System.out.println("Mid的静态初始化块");
  18.     }
  19.     {
  20.         System.out.println("Mid的普通初始化块");
  21.     }
  22.     public Mid()
  23.     {
  24.         System.out.println("Mid的无参数的构造器");
  25.     }
  26.     public Mid(String msg)
  27.     {
  28.         //通过this调用同一类中重载的构造器
  29.         this();
  30.         System.out.println("Mid的带参数构造器,其参数值:" + msg);
  31.     }
  32. }
  33. class Leaf extends Mid
  34. {
  35.     static{
  36.         System.out.println("Leaf的静态初始化块");
  37.     }
  38.     {
  39.         System.out.println("Leaf的普通初始化块");
  40.     }   
  41.     public Leaf()
  42.     {
  43.         //通过super调用父类中有一个字符串参数的构造器
  44.         super("Struts2权威指南");
  45.         System.out.println("执行Leaf的构造器");
  46.     }
  47. }
  48. public class Test
  49. {
  50.     public static void main(String[] args) 
  51.     {
  52.         new Leaf();
  53.         new Leaf();
  54.     }
  55. }

本章练习

1.定义普通人,老师,班主任,学生,学校,提供适当属性和方法。。。。(这题比较空,我就用rose搞下)

 

2.上题用组合实现(也就把普通人对象通过构造传入,这太无聊了就不做了)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值