Animal类
public 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();
}
swim接口类
public interface swim {
public abstract void swim();
}
rabbit子类
public class rabbit extends Animal{
public rabbit() {
}
public rabbit(String name, int age) {
super(name, age);
}
@Override
public void eat() {
System.out.println("兔子在吃胡萝卜");
}
}
frog子类
public class frog extends Animal implements swim{
public frog() {
}
public frog(String name, int age) {
super(name, age);
}
@Override
public void eat() {
System.out.println("青蛙在吃虫子");
}
@Override
public void swim() {
System.out.println("青蛙在蛙泳");
}
}
Dog子类
public class Dog extends Animal implements swim{
public Dog() {
}
public Dog(String name, int age) {
super(name, age);
}
@Override
public void eat() {
System.out.println("狗在吃骨头");
}
@Override
public void swim() {
System.out.println("狗在狗刨");
}
}
测试类
public class Teat {
public static void main(String[] args) {
rabbit r = new rabbit("小白", 2);
System.out.println(r.getName()+", "+r.getAge());
r.eat();
System.out.println("----------------------");
frog f =new frog("小青",1);
System.out.println(f.getName()+", "+f.getAge());
f.eat();
f.swim();
System.out.println("----------------------");
Dog d=new Dog("小汪",3);
System.out.println(d.getName()+", "+d.getAge());
d.eat();
d.swim();
}
}
运行结果
小白, 2
兔子在吃胡萝卜
----------------------
小青, 1
青蛙在吃虫子
青蛙在蛙泳
----------------------
小汪, 3
狗在吃骨头
狗在狗刨