各位老师,求解,Java代码 急用,谢谢!
展开全部
这题目设计的Lady类有点多此一举,代码如下:
public abstract class Animal {
private String name;
public Animal(String name){
this.name = name;
}
abstract void enjoy();
}
public class 袭庆Dog extends Animal {
private String hair_color;
public Dog(String name, String hair_color) {
super(name);
this.hair_color = hair_color;
}
@Override
void enjoy() {
System.out.println("汪汪汪~");
}
}
public class Cat extends Animal 拍顷握{
private String eyes_color;
public Cat(String name, String eyes_color) {
super(name);
this.eyes_color = eyes_color;
}
@Override
void enjoy() 乎弊{
System.out.println("喵喵喵~");
}
}
public class Lady {
private String name;
private Animal[] pets;
public Lady(String name, Animal[] pets) {
this.name = name;
this.pets = pets;
}
public void myPetEnjoy(){
for (int i = 0; i < pets.length; i++) {
pets[i].enjoy();
}
}
}
public class TestAnimal {
public static void main(String[] args) {
Cat cat = new Cat("小黑狗", "黑色");
Dog dog = new Dog("小花猫", "黄色");
Animal[] pets = {cat, dog};
Lady lady = new Lady("张三", pets);
lady.myPetEnjoy();
}
}
//输出如下:
喵喵喵~
汪汪汪~
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询