class Demo22_Animal {
public static void main(String[] agrs) {
Animal c = new Cat("笨猫",5);
c.eat();
c.sleep();
System.out.println("---------------------------");
Cat c1 = new JumpCat("飞天猫",4);
c1.eat();
c1.sleep();
JumpCat jc = (JumpCat)c1;
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();
}
interface Jumping {
public void jump();
}
class Cat extends Animal {
public Cat() {}
public Cat(String name,int age) {
super(name,age);
System.out.println("名字叫:"+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 eat() {
System.out.println("爱吃跳跳糖");
}
public void sleep() {
System.out.println("趴着睡觉");
}
public void jump() {
System.out.println("爱跳高");
}
}
|
|