class Demo_Animal {
public static void main(String[] args) {
Cat c = new Cat("花花",3);
System.out.println(c.getName()+"-----------"+c.getAge());
c.eat();
c.catchMouse();
JumpCat jc = new JumpCat("加菲", 2);
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 getName() { //获取姓名
return name;
}
public void setAge(int age) { //设置年龄
this.age = age;
}
public int getAge() { //获取年龄
return age;
}
public abstract void eat(); //吃抽象类
public abstract void sleep(); //睡抽象类
}
//猫类
class Cat extends Animal {
public Cat() {} //空参构造
public Cat(String name, int age) { //带参构造
super(name, age);
}
public void eat() { //重写吃抽象类
System.out.println("猫吃鱼");
}
public void catchMouse() { //猫特有的方法
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 lookHome() { //猫特有的方法
System.out.println("看家");
}
public void sleep(){ //重写睡抽象类
System.out.println("狗卷着睡");
}
}
interface Jump {
public abstract void jump();
}
class JumpCat extends Cat implements Jump {
public JumpCat() {} //空参构造
public JumpCat(String name, int age) { //带参构造
super(name, age);
}
public void jump() {
System.out.println("猫跳高");
}
} |
|