class Demo9999_Animal {
public static void main(String[] args) {
Cat c = new Cat("花花",2);
System.out.println(c.getName()+"..."+c.getAge());
c.eat();
c.sleep();
JumpCat jc = new JumpCat("跳高猫",3);
System.out.println(jc.getName()+"..."+jc.getAge());
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 getName() { //获取姓名
return name;
}
public void setAge(int age ) {
this.age = age;
}
public int getAge() {
return age;
}
public abstract void eat(); //抽象方法1
public abstract void sleep(); //抽象方法2
}
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("猫跳高");
}
}
|
|