Question 5 Design the Bird and Fish classes to inherit from the abstract class Animal, implement its abstract method info(), and output their information .
package com.homework.demo.test4_5; /** * animal * @author suixin * */ public abstract class Animal { private int age; //age /** * no-argument constructor */ public Animal() { super(); // TODO Auto-generated constructor stub } /** * Construct with parameters * @param age age */ public Animal(int age) { super(); this.age = age; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } /** * Abstract output information method */ public abstract void info(); } package com.homework.demo.test4_5; /** * birds * @author suixin * */ public class Bird extends Animal { private String color = "red"; //color public String getColor() { return color; } public void setColor(String color) { this.color = color; } /** * No-argument constructor */ public Bird() { super(); // TODO Auto-generated constructor stub } /** * Constructor with parameters * @param age age */ public Bird(int age) { super(age); // TODO Auto-generated constructor stub } /** * Override the parent class info() method */ @Override public void info() { // TODO Auto-generated method stub System.out.println("I am a bird of "+this.color+"!\n"+"This year"+super.getAge()+" is old!\n"); } } package com.homework.demo.test4_5; /** * fish * @author suixin * */ public class Fish extends Animal { private int swight = 5; //weight public int getSwight() { return swight; } public void setSwight(int swight) { this.swight = swight; } /** * No-argument constructor */ public Fish() { super(); // TODO Auto-generated constructor stub } /** * Constructor with parameters * @param age age */ public Fish(int age) { super(age); // TODO Auto-generated constructor stub } /** * Override the parent class info() method */ @Override public void info() { // TODO Auto-generated method stub System.out.println("I am a "+this.swight+" heavy fish!\n"+"This year"+super.getAge()+" is old!\n\n"); } } package com.homework.demo.test4_5; /** * test class * @author suixin * */ public class Test { public static void main(String[] args) { // TODO Auto-generated method stub Bird bird = new Bird(4); //The actual parameter of age is 4 to construct an object bird.info(); Fish fish = new Fish(2); //The actual parameter of age is 4 to construct an object fish.info(); } }
Question 6 There are two poultry in the house: a chicken and a duck. Please describe it with the characteristics of object-oriented thinking of encapsulation and inheritance.
package com.homework.demo.test4_6; /** * animal * @author suixin * */ public abstract class Animal1 { private String name; //姓名 private String strain; //variety public String getName() { return name; } public void setName(String name) { this.name = name; } public String getStrain() { return strain; } public void setStrain(String strain) { this.strain = strain; } /** * No-argument constructor */ public Animal1() { super(); // TODO Auto-generated constructor stub } /** * Constructor with parameters * @param name name * @param strain variety */ public Animal1(String name, String strain) { super(); this.name = name; this.strain = strain; } public abstract void print(); //Abstract method, output information } package com.homework.demo.test4_6; /** * Chicken * @author suixin * */ public class Chicken extends Animal1 { /** * No-argument constructor */ public Chicken() { super(); // TODO Auto-generated constructor stub } /** * Constructor with parameters * @param name name * @param strain variety */ public Chicken(String name, String strain) { super(name, strain); // TODO Auto-generated constructor stub } /** * Override the parent class print() method */ @Override public void print() { // TODO Auto-generated method stub System.out.println("My name is "+super.getName()+", it's a "+super.getStrain()+"\nI like to eat bugs!\nI will croak!\n"); } } package com.homework.demo.test4_6; /** * Duck * @author suixin * */ public class Duck extends Animal1 { /** * No-argument constructor */ public Duck() { super(); // TODO Auto-generated constructor stub } /** * Constructor with parameters * @param name name * @param strain variety */ public Duck(String name, String strain) { super(name, strain); // TODO Auto-generated constructor stub } /** * Override the parent class print() method */ @Override public void print() { // TODO Auto-generated method stub System.out.println("My name is "+super.getName()+", it's a "+super.getStrain()+"\nI like to eat small fish!\nI can swim!"); } } package com.homework.demo.test4_6; /** * test class * @author suixin * */ public class TestAnimal1 { public static void main(String[] args) { // TODO Auto-generated method stub Chicken chicken = new Chicken("Ooh","Luhua chicken"); //Construct object with parameters chicken.print(); //call the print method Duck duck = new Duck("Quack","Spot-billed duck"); //Construct object with parameters duck.print(); //call the print method } }