class Day08WorkTest {
public static void main(String[] args) {
Dog d = new Dog("小黑","黑色",6);
System.out.println(d.getName()+" 花色:"+d.getColor()+" 年龄:"+d.getAge());
d.eat();
d.lookHome();
System.out.println("-------------------------------------------------------");
Cat c = new Cat("小花","花",4);
System.out.println(c.getName()+" 花色:"+c.getColor()+" 年龄:"+c.getAge());
c.eat();
c.catchMouse();
}
}
class Animal {
private String name;
private String color;
private int age;
public Animal() {}
public Animal(String name,String color,int age) {
this.name = name;
this.color = color;
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setColor(String color) {
this.color = color;
}
public String getColor() {
return color;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
public void sleep() {
System.out.println("睡觉");
}
}
class Cat extends Animal {
public Cat() {}
public Cat(String name, String color, int age) {
super(name, color, age);
}
public void eat() {
System.out.println("吃鱼");
}
public void catchMouse() {
System.out.println("抓老鼠");
}
}
class Dog extends Animal {
public Dog() {}
public Dog(String name, String color, int age) {
super(name, color, age);
}
public void eat() {
System.out.println("吃肉");
}
public void lookHome() {
System.out.println("看家");
}
}
|
|