Reuse software using inheritance
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 () + "the heart keeps beating... "); 9} 10 public void breath () {11 beat (); 12 system. Out. println (this. GetType () +" breathe... "); 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 () + "Flying freely in the sky"); 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 () + "Flying freely in the sky"); 34} 35} 36 public class animaltest 37 {38 public static void main (string [] ARGs) 39 {40 bird B = new bird ("bird"); 41 B. breath (); 42 Wolf c = new wolf ("wolf"); 43 C. breath (); 44} 45}Software reuse using combinations
2 class animal // The method or filed in this class should be 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 () + "the heart keeps beating... "); 10} 11 Public void breath () {12 beat (); 13 system. Out. println (this. GetType () +" breathe... "); 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 = A1 = [email protected] 24} 26 Public void breath () {27. breath (); // a1.bteath (); 28} 29 public void fly () {30 system. out. println (this. a ). getType () + "Flying freely in the sky"); 31} 32} 33 class wolf34 {35 private animal a; 36 Public Wolf (animal a) {37 this. A = A; // This. A = a2 = [email protected] 38} 39 public void breath () {40. breath (); 41} 42 Public void run () {43 system. out. println (this. a ). getType () + "freely run on land"); 44} 46} 47 Public class compositetest48 {49 public static void main (string [] ARGs) 50 {51 animal a1 = new animal ("bird"); 52 bird B = new bird (A1); 53 B. breath (); 54 B. fly (); 55 animal a2 = new animal ("wolf"); 56 Wolf c = new wolf (A2); 57 C. breath (); 58 C. run (); 59} 60}
Software reuse using inheritance and combination