class Demo_Animal {
public static void main(String[] args) {
Cat c = new Cat("花", 4);
System.out.println(c.getColor()+"----"+c.getLeg());
c.eat();
System.out.println("---------------------------");
Dog d = new Dog("黑", 4);
System.out.println(d.getColor()+"----"+d.getLeg());
d.eat();
}
}
class Animal {
private String color;
private int leg;
public Animal() {}
public Animal(String color, int leg) {
this.color = color;
this.leg = leg;
}
public void setColor(String color) {
this.color = color;
}
public String getColor() {
return color;
}
public void setLeg(int leg) {
this.leg = leg;
}
public int getLeg() {
return leg;
}
public void eat() {
System.out.println("吃饭");
}
}
class Cat extends Animal{
public Cat() {}
public Cat(String color, int leg) {
super(color, leg);
}
public void eat() {
System.out.println("猫吃鱼");
}
}
class Dog extends Animal{
public Dog() {}
public Dog(String color, int leg) {
super(color, leg);
}
public void eat() {
System.out.println("够吃肉");
}
} |
|