class Demo {
public static void main(String[] args) {
Cat sc = new Cat();
sc.setColor("白色");
sc.setLeg(4);
sc.show();
sc.eat();
sc.run();
System.out.println();
Dog st = new Dog("黑色", 4);
st.show();
st.eat();
sc.run();
}
}
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 this.color;
}
public void setLeg(int leg){
this.leg = leg;
}
public int getLeg(){
return this.leg;
}
public void eat(){
System.out.println("动物都是要吃东西的");
}
public void run(){
System.out.println("动物都是用腿跑的");
}
}
class Cat extends Animal{
public Cat(){}
public Cat(String color, int leg){
super(color,leg);
}
public void eat(){
System.out.println("猫猫爱吃鱼");
}
public void show(){
System.out.println(getColor() + "猫猫" + getLeg() +"腿");
}
}
class Dog extends Animal{
public Dog(){}
public Dog(String color,int leg){
super(color,leg);
}
public void eat(){
System.out.println("狗狗爱吃肉");
}
public void show(){
System.out.println(getColor() + "狗狗" + getLeg() +"腿");
}
} |