java程序设计:设计bird类,fish类,都继承自Animal,实现其方法printInfo(),输出信息.
public class Test {
public static void main(String[] args) {
Bird b = new Bird("红色", 4);
Fish f = new Fish("5", 2);
b.printInfo();
f.printInfo();
}
}
public abstract class Animal {
String color;
String weight;
int age;
public abstract void printInfo();
}
public class Bird extends Animal {
Bird(String color, int age) {
super.color = color;
super.age = age;
}
@Override
public void printInfo() {
System.out.println("我是一只" + color + "的鸟!");
System.out.println("今年" + age + "岁了!");
}
}
public class Fish extends Animal {
Fish(String weight, int age) {
super.weight = weight;
super.age = age;
}
@Override
public void printInfo() {
System.out.println("我是一条" + weight + "斤重的鱼!");
System.out.println("今年" + age + "岁了!");
}
}