猿问

关于写三个子类继承并重写eat方法,写boy类有一个feed方法喂养动物Animal作参数

求写的详细简单易懂点,谢谢

回首忆惘然
浏览 225回答 2
2回答

凤凰求蛊

你好,标准答案是这样的:abstract class Animal {public abstract void eat();}class Rabbit extends Animal {public void eat() {System.out.println("我吃草");}}class Monkey extends Animal {public void eat(){System.out.println("我吃香蕉");}}class Tiger extends Animal {public void eat(){System.out.println("我吃肉");}}class Boy{public void feed(Animal animal){animal.eat() ;}}public class TestAnimal {public static void main(String[] args) {Animal animal1 = new Rabbit() ;Animal animal2 = new Monkey() ;Animal animal3 = new Tiger() ;Boy boy = new Boy() ;boy.feed(animal1) ;boy.feed(animal2) ;boy.feed(animal3) ;}}

慕哥9229398

abstract class Animal{ Animal(){}public void eat();}class monkey extends Animal{ public void eat(){System.out.println("我吃香蕉");}}classTiger extends Animal{ public void eat(){System.out.println(”我吃猴子“);}}class boy{ public static void main(String args[]){Aniaml animal1=new monkey();Aniaml animal2=new Tiger();animal1.eat();animal2.eat();}}
随时随地看视频慕课网APP
我要回答