使用(接口)设计一个动物声音"模拟器",希望模拟器可以模拟许多动物声音
interface Animal{
public void cry();
public String getname();
}
class dog implements Animal{
public void cry(){
System.out.println("汪!汪汪!");
}
public String getname(){
return"狗";
}
}
class cat implements Animal{
public void cry(){
System.out.println("喵!喵喵!");
}
public String getname(){
return"猫";
}
}
class Simulator{
void Playsound(Animal animal){
System.out.print("现在是"+animal.getname()+"的声音:");
animal.cry();
}
}
public class text{
public static void main(String args[]){
Simulator simulator=new Simulator();
simulator.Playsound(new dog());
simulator.Playsound(new cat());
}
}