class TestAnimal {
public static void main(String[] args) {
Cat c1 = new JumpCat("跳高猫",3);
eat(c1);
JumpCat c2 = (JumpCat)c1;
eat(c2);
c2.jump();
Dog d1 = new JumpDog("跳高狗",5);
eat(d1);
JumpDog d2 = (JumpDog)d1;
eat(d2);
d2.jump();
/*JumpCat jc = new JumpCat("跳高猫",3);
eat(jc);
jc.jump();
JumpDog jd = new JumpDog("跳高狗",5);
eat(jd);
jd.jump();*/
}
public static void eat(Animal a) {
a.eat();
}
}
abstract class Animal {
private String name;
private int age;
public Animal() {}
public Animal(String name, int age) {
this.name = name;
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
public abstract void eat();
}
interface Jump {
public void jump();
}
class Cat extends Animal {
public Cat() {}
public Cat(String name, int age) {
super(name,age);
}
public void eat() {
System.out.println("猫吃鱼");
}
}
class JumpCat extends Cat implements Jump {
public JumpCat() {}
public JumpCat(String name, int age) {
super(name,age);
}
public void jump() {
System.out.println("能跳很高的猫");
}
}
class Dog extends Animal {
public Dog() {}
public Dog(String name, int age) {
super(name,age);
}
public void eat() {
System.out.println("狗吃骨头");
}
}
class JumpDog extends Dog implements Jump {
public JumpDog() {}
public JumpDog(String name, int age) {
super(name,age);
}
public void jump() {
System.out.println("能跳很高的狗");
}
} |