public interface Animal {
void cry();
String getAnimalName();
}
public class Dog implements Animal {
public void cry(){
System.out.println("狗的叫声");
}
public String getAnimalName(){
return "小狗";
}
}
public class Cat implements Animal{
public void cry(){
System.out.println("猫的声音");
}
public String getAnimalName(){
return "小猫";
}
}
public class Simulator {
void playSound(Animal animal){
if(animal instanceof Dog){
animal.cry();
}
if(animal instanceof Cat)
animal.cry();
}
}
public class Application {
public static void main(String[] args) {
// TODO 自动生成的方法存根
Simulator simulator=new Simulator();
simulator.playSound(new Dog());
simulator.playSound(new Cat());
}
}