不对,因为猫和狗吃饭睡觉不同,建议在Animal类中定义为抽象类。参考代码如下:
abstract class Animal {
private String name;
private int age;
public Animal() {} //无参构造方法
public Animal(String name,int age) {
this.name = name;
this.age = age;
}
//抽象方法吃饭eat()
public abstract void eat();
//抽象方法睡觉sleep()
public abstract void sleep();
}
interface Jump {
//jump()
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 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("跳高狗跳高.");
}
}
|