利用繼承和組合實現軟體複用

來源:互聯網
上載者:User

標籤:style   blog   color   os   ar   div   sp   c   log   

利用繼承實現軟體複用
 1 class Animal 2 { 3     private String type; 4     public Animal(String type){ 5         this.type = type; 6     } 7     public void beat(){ 8         System.out.println(this.getType()+"心臟在不停跳動。。。"); 9     }10     public void breath(){11         beat();12         System.out.println(this.getType()+"呼吸中。。。");13     }14     public String getType(){15         return this.type;16     }17 }18 class Bird extends Animal19 {        20     public Bird(String type){21         super(type);22     }23     public void fly(){24         System.out.println(this.getType()+"在天空自由自在的飛翔");25     }26 }27 class Wolf extends Animal28 {        29     public Wolf(String type){30         super(type);31     }32     public void run(){33         System.out.println(this.getType()+"在天空自由自在的飛翔");34     }35 }36 public class AnimalTest 37 {38     public static void main(String[] args)39     {40         Bird b = new Bird("鳥");41         b.breath();42         Wolf c = new Wolf("狼");43         c.breath();44     }45 }
利用組合實現軟體複用
 2 class Animal    //該類裡的方法或Filed應是複用對象共同擁有的 3 { 4     private String type; 5     public Animal(String type){ 6         this.type = type; 7     } 8     public void beat(){ 9         System.out.println(this.getType()+"心臟在不停跳動。。。");10     }11     public void breath(){12         beat();13         System.out.println(this.getType()+"呼吸中。。。");14     }15     public String getType(){16         return this.type;17     }18 }19 class Bird20 {21     private Animal a;        22     public Bird(Animal a){23         this.a = a;        //this.a = a = a1 = [email protected]24 }26     public void breath(){27         a.breath();        //a1.bteath();28     }29     public void fly(){30         System.out.println((this.a).getType()+"在天空自由自在的飛翔");31     }32 }33 class  Wolf34 {35     private Animal a;36     public Wolf(Animal a){37         this.a = a;        //this.a = a = a2 = [email protected]38     }39     public void breath(){40         a.breath();41     }42     public void run(){43         System.out.println((this.a).getType()+"在陸地上自由奔跑");44     }46 }47 public class CompositeTest48 {49     public static void main(String[] args)50     {51         Animal a1 = new Animal("鳥");52         Bird b = new Bird(a1);53         b.breath();54         b.fly();55         Animal a2 = new Animal("狼");56         Wolf c = new Wolf(a2);57         c.breath();58         c.run();59     }    60 }

 

利用繼承和組合實現軟體複用

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.