已知动物类(姓名,年龄,吃饭,睡觉),它有两个子类,分别是猫类和狗类。
不同的是猫吃鱼,狗吃肉。
经过马戏团的培训后,部分猫会跳高,部分狗也会跳高。
请写代码实现。
class Test5_Animal {
public static void main(String[] args) {
Cat c = new Cat("加菲",3);
c.eat();
c.sleep();
System.out.println("----------");
JumpCat jc = new JumpCat("汤姆",4);
jc.eat();
jc.sleep();
jc.jump();
}
}
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 name(){
return name;
}
public void setAge(int age){
this.age = age;
}
public int getAge(){
return age;
}
public abstract void eat();
public abstract void sleep();
}
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("猫吃鱼");
}
public void sleep(){
System.out.println("侧着睡");
}
} |
|