|--需求說明ide
Java 設計鳥類Bird 魚類Fish 都繼承自抽象的動物類Animal, 實現其抽象方法Info 輸出各自信息測試
|--實現思路this
|--代碼內容spa


1 /** 2 * @auther::9527 3 * @Description: 動物類 4 * @program: Port 5 * @create: 2019-07-17 22:22 6 */ 7 public abstract class Animal { 8 private String color; 9 private String type; 10 private int age; 11 private int weight; 12 13 public String getColor() { 14 return color; 15 } 16 17 public void setColor(String color) { 18 this.color = color; 19 } 20 21 public String getType() { 22 return type; 23 } 24 25 public void setType(String type) { 26 this.type = type; 27 } 28 29 public int getAge() { 30 return age; 31 } 32 33 public void setAge(int age) { 34 this.age = age; 35 } 36 37 public int getWeight() { 38 return weight; 39 } 40 41 public void setWeight(int weight) { 42 this.weight = weight; 43 } 44 45 public abstract void info(); 46 }


1 /** 2 * @auther::9527 3 * @Description: 4 * @program: Port 5 * @create: 2019-07-17 22:32 6 */ 7 public class Fish extends Animal { 8 @Override 9 public String getColor() { 10 return super.getColor(); 11 } 12 13 @Override 14 public void setColor(String color) { 15 super.setColor(color); 16 } 17 18 @Override 19 public String getType() { 20 return super.getType(); 21 } 22 23 @Override 24 public void setType(String type) { 25 super.setType(type); 26 } 27 28 @Override 29 public int getAge() { 30 return super.getAge(); 31 } 32 33 @Override 34 public void setAge(int age) { 35 super.setAge(age); 36 } 37 38 @Override 39 public int getWeight() { 40 return super.getWeight(); 41 } 42 43 @Override 44 public void setWeight(int weight) { 45 super.setWeight(weight); 46 } 47 48 @Override 49 public void info() { 50 System.out.println("我是一條"+getWeight()+"重的"+getType()); 51 System.out.println("今年"+getAge()+"歲了"); 52 } 53 }


1 /** 2 * @auther::9527 3 * @Description: 鳥類 4 * @program: Port 5 * @create: 2019-07-17 22:23 6 */ 7 public class Bird extends Animal { 8 9 @Override 10 public String getColor() { 11 return super.getColor(); 12 } 13 14 @Override 15 public void setColor(String color) { 16 super.setColor(color); 17 } 18 19 @Override 20 public String getType() { 21 return super.getType(); 22 } 23 24 @Override 25 public void setType(String type) { 26 super.setType(type); 27 } 28 29 @Override 30 public int getAge() { 31 return super.getAge(); 32 } 33 34 @Override 35 public void setAge(int age) { 36 super.setAge(age); 37 } 38 39 @Override 40 public int getWeight() { 41 return super.getWeight(); 42 } 43 44 @Override 45 public void setWeight(int weight) { 46 super.setWeight(weight); 47 } 48 49 @Override 50 public void info() { 51 System.out.println("我是一隻"+getColor()+"的"+getType()); 52 System.out.println("今年"+getAge()+"歲了"); 53 } 54 }


1 /** 2 * @auther::9527 3 * @Description: 測試類 4 * @program: Port 5 * @create: 2019-07-17 22:34 6 */ 7 public class Test { 8 public static void main(String[] args) { 9 //父類引用指向子類對象,把鳥類的對象賦值到父類Animal上面 10 Animal animal = new Bird(); 11 animal.setColor("紅色"); 12 animal.setType("鳥"); 13 animal.setAge(4); 14 animal.info(); 15 16 System.out.println("\n****---****---****\n"); 17 18 //父類引用指向子類對象,把魚類的對象賦值到父類Animal上面 19 Animal animal1 = new Fish(); 20 animal1.setWeight(5); 21 animal1.setType("魚"); 22 animal1.setAge(2); 23 animal1.info(); 24 25 } 26 }
|--運行結果設計