请老师检查代码是否符合要求?
Animal类:
package com.imooc.exercise6; public abstract class Animal { private String species; private String name; public Animal() { } public Animal(String species, String name) { this.species = species; this.name = name; } public String getSpecies() { return species; } public void setSpecies(String species) { this.species = species; } public String getName() { return name; } public void setName(String name) { this.name = name; } public abstract void play(); }
Dog类:
package com.imooc.exercise6; public class Dog extends Animal{ public Dog() { } public Dog(String species, String name) { super(species, name); } @Override public void play() { System.out.println(super.getSpecies()+super.getName()+"在做游戏!"); } }
Cat类:
package com.imooc.exercise6; public class Cat extends Animal{ public Cat() { } public Cat(String species, String name) { super(species, name); } @Override public void play() { System.out.println(super.getSpecies()+super.getName()+"在做游戏!"); } }
AnimalPlay类:
package com.imooc.exercise6; import java.util.List; public class AnimalPlay { public void playAnimal(List<? extends Animal> animals){ for(Animal animal:animals) animal.play(); } }
Test类:
package com.imooc.exercise6; import java.util.ArrayList; import java.util.List; public class Test { public static void main(String[] args) { List<Dog> dogs=new ArrayList<Dog>(); dogs.add(new Dog("小狗","巴迪")); dogs.add(new Dog("小狗","豆豆")); List<Cat> cats=new ArrayList<Cat>(); cats.add(new Cat("小猫","花花")); cats.add(new Cat("小猫","凡凡")); AnimalPlay animalPlay=new AnimalPlay(); animalPlay.playAnimal(dogs); animalPlay.playAnimal(cats); } }
16
收起
正在回答
1回答
同学你好,代码完成的很好,符合要求!继续努力!如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
1. Java 零基础入门
- 参与学习 人
- 提交作业 3802 份
- 解答问题 11489 个
本阶段带你迈入Java世界,学习Java必备基础知识,基础语法、面向对象思想以及常用工具类的使用。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星