class Demo1_Abstract {
public static void main(String[] args) {
JumpCat jc = new JumpCat();
jc.eat();
jc.sleep();
jc.jump();
JumpDog jd = new JumpDog();
jd.eat();
jd.sleep();
jd.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 String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public abstract void eat();
public void sleep() {
System.out.println("睡觉");
}
}
class Cat extends Animal{
public Cat() {
}
public Cat(String name, int age) {
super(name, age);
}
public void eat() {
System.out.println("猫吃鱼");
}
}
class Dog extends Animal {
public Dog() {
}
public Dog(String name, int age) {
super(name, age);
}
public void eat() {
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("猫跳高");
}
}
class JumpDog extends Dog implements Jump {
public JumpDog() {
}
public JumpDog(String name, int age) {
super(name, age);
}
public void jump() {
System.out.println("狗跳高");
}
}
|
|