package LeiDeJiCheng; 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 abstract void eat(); public String getName(){ return name; } public void setName(String name){ this.name = name; } public int getAge(){ return age; } } class Rabbit extends Animal{ public Rabbit(){ } public Rabbit(String name,int age){ super(name, age); } @Override public void eat(){ System.out.println("兔子吃胡萝卜"); } } class Dog extends Animal{ public Dog(){} public Dog(String name,int age){ super(name, age); } @Override public void eat(){ System.out.println("狗吃shit"); } } class Friog extends Animal { public Friog(){} public Friog(String name,int age){ super(name,age); } @Override public void eat(){ System.out.println("青蛙吃虫子"); } }
时间: 2023-06-01 18:03:59 浏览: 194
//测试类
public class Test {
public static void main(String[] args) {
Animal animal1 = new Rabbit("小白兔", 2);
Animal animal2 = new Dog("旺财", 3);
Animal animal3 = new Friog("青蛙", 1);
animal1.eat();
animal2.eat();
animal3.eat();
}
}
//输出:
//兔子吃胡萝卜
//狗吃shit
//青蛙吃虫子
阅读全文