class Demo_Animal {
public static void main(String[] args) {
JumpCat c = new JumpCat("加菲",8);
System.out.println("姓名:" + c.getName() + ",年龄:" + c.getAge());
c.eat();
c.sleep();
c.jump();
System.out.println("-----------------------------------------------");
JumpDog d = new JumpDog("小一",4);
System.out.println("姓名:" + d.getName() + ",年龄:" + d.getAge());
d.eat();
d.sleep();
d.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 void eat() { //吃饭
System.out.println("吃饭");
}
public void sleep() { //睡觉
System.out.println("睡觉");
}
}
interface Jumping { //跳高的接口
public abstract 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 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 JumpCat extends Cat implements Jumping{
public JumpCat() {} //空参构造
public JumpCat(String name,int age) { //有参构造
super(name,age);
}
public void jump() { //实现接口,猫跳高
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("狗跳高");
}
} |