学生喂养三种宠物:猫、狗和鸟
动物类(Animal):属性(name、age)、方法(speak、move、eat)
猫类(Cat)继承动物类,添加新方法(play)
狗类(Dog)继承动物类,添加新方法(play)
鸟类(Bird)继承动物类,添加新方法(play)
学生类(Student):属性(name)、方法(feed(animal))
(一)编程实现
public class Animal {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void speak() {
System.out.println(name + "今年" + age + "岁了。");
}
public void move() {
System.out.println(name + "在动……");
}
public void eat() {
System.out.println(name + "在吃……");
}
@Override
public String toString() {
return "Animal{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}