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