class Dog extends Animal implements IAbility{
public Dog(String name, int age) {
super(name, age);
}
@Override
public void showInfo() {
System.out.println("我是一只狗,我的名字叫" + getName() + ",今年" + getAge() + "岁");
}
@Override
public void cry() {
System.out.println("旺旺");
}
}
class Cat extends Animal implements IAbility{
public Cat(String name, int age) {
super(name, age);
}
@Override
public void showInfo() {
System.out.println("我是一只猫,我的名字叫"+getName() + ",今年" + getAge() + "岁");
}
@Override
public void cry() {
System.out.println("喵喵");
}
}
// Simulator类
class Simulator {
public void playSound(IAbility animal) {
animal.showInfo();
animal.cry();
Animal a = (Animal) animal;
System.out.println(a.getName());
System.out.print(a.getAge());
}
}
运行结果跟答案一样,但是提交答案时老是说有错误