设计鸟类Bird、 鱼类Fish ,都继承自抽象的动物类Animal, 实现其抽象方法Info,输出各自信息。
代码如下:
Animal类
public abstract class Animal {private String form;private int age;public String getForm() {return form;}public void setForm(String form) {this.form = form;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public abstract void info();
}
Bird类
public class Bird extends Animal {public void info() {super.setForm("红色的");super.setAge(4);System.out.println("我是一只" + getForm() + "鸟!" + "\n我今年" + getAge() + "岁了!");}}
Fish类
public class Fish extends Animal{public void info() {super.setForm("5斤重的");super.setAge(2);System.out.println("我是一只" + getForm() + "鱼!" + "\n我今年" + getAge() + "岁了!");}}
测试类
public class AnimalTest {public static void main(String[] args) {Bird bird = new Bird();bird.info();System.out.println();Fish fish = new Fish();fish.info();}}
运行结果