|--需求说明
Java 设计鸟类Bird 鱼类Fish 都继承自抽象的动物类Animal, 实现其抽象方法Info 输出各自信息
|--实现思路
|--代码内容

1 1 /** 2 2 * @auther::9527 3 3 * @Description: 动物类 4 4 * @program: Port 5 5 * @create: 2019-07-17 22:22 6 6 */ 7 7 public abstract class Animal { 8 8 private String color; 9 9 private String type; 1010 private int age; 1111 private int weight; 1212 1313 public String getColor() { 1414 return color; 1515 } 1616 1717 public void setColor(String color) { 1818 this.color = color; 1919 } 2020 2121 public String getType() { 2222 return type; 2323 } 2424 2525 public void setType(String type) { 2626 this.type = type; 2727 } 2828 2929 public int getAge() { 3030 return age; 3131 } 3232 3333 public void setAge(int age) { 3434 this.age = age; 3535 } 3636 3737 public int getWeight() { 3838 return weight; 3939 } 4040 4141 public void setWeight(int weight) { 4242 this.weight = weight; 4343 } 4444 4545 public abstract void info(); 4646 }
抽象类---动物类

1 1 /** 2 2 * @auther::9527 3 3 * @Description: 4 4 * @program: Port 5 5 * @create: 2019-07-17 22:32 6 6 */ 7 7 public class Fish extends Animal { 8 8 @Override 9 9 public String getColor() { 1010 return super.getColor(); 1111 } 1212 1313 @Override 1414 public void setColor(String color) { 1515 super.setColor(color); 1616 } 1717 1818 @Override 1919 public String getType() { 2020 return super.getType(); 2121 } 2222 2323 @Override 2424 public void setType(String type) { 2525 super.setType(type); 2626 } 2727 2828 @Override 2929 public int getAge() { 3030 return super.getAge(); 3131 } 3232 3333 @Override 3434 public void setAge(int age) { 3535 super.setAge(age); 3636 } 3737 3838 @Override 3939 public int getWeight() { 4040 return super.getWeight(); 4141 } 4242 4343 @Override 4444 public void setWeight(int weight) { 4545 super.setWeight(weight); 4646 } 4747 4848 @Override 4949 public void info() { 5050 System.out.println("我是一条"+getWeight()+"重的"+getType()); 5151 System.out.println("今年"+getAge()+"岁了"); 5252 } 5353 }
具体类--鱼类

1 1 /** 2 2 * @auther::9527 3 3 * @Description: 鸟类 4 4 * @program: Port 5 5 * @create: 2019-07-17 22:23 6 6 */ 7 7 public class Bird extends Animal { 8 8 9 9 @Override 1010 public String getColor() { 1111 return super.getColor(); 1212 } 1313 1414 @Override 1515 public void setColor(String color) { 1616 super.setColor(color); 1717 } 1818 1919 @Override 2020 public String getType() { 2121 return super.getType(); 2222 } 2323 2424 @Override 2525 public void setType(String type) { 2626 super.setType(type); 2727 } 2828 2929 @Override 3030 public int getAge() { 3131 return super.getAge(); 3232 } 3333 3434 @Override 3535 public void setAge(int age) { 3636 super.setAge(age); 3737 } 3838 3939 @Override 4040 public int getWeight() { 4141 return super.getWeight(); 4242 } 4343 4444 @Override 4545 public void setWeight(int weight) { 4646 super.setWeight(weight); 4747 } 4848 4949 @Override 5050 public void info() { 5151 System.out.println("我是一只"+getColor()+"的"+getType()); 5252 System.out.println("今年"+getAge()+"岁了"); 5353 } 5454 }
具体类--鸟类

1 1 /** 2 2 * @auther::9527 3 3 * @Description: 测试类 4 4 * @program: Port 5 5 * @create: 2019-07-17 22:34 6 6 */ 7 7 public class Test { 8 8 public static void main(String[] args) { 9 9 //父类引用指向子类对象,把鸟类的对象赋值到父类Animal上面 1010 Animal animal = new Bird(); 1111 animal.setColor("红色"); 1212 animal.setType("鸟"); 1313 animal.setAge(4); 1414 animal.info(); 1515 1616 System.out.println("\n****---****---****\n"); 1717 1818 //父类引用指向子类对象,把鱼类的对象赋值到父类Animal上面 1919 Animal animal1 = new Fish(); 2020 animal1.setWeight(5); 2121 animal1.setType("鱼"); 2222 animal1.setAge(2); 2323 animal1.info(); 2424 2525 } 2626 }
测试类
|--运行结果
