package simulatorInterface;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Simulator simulator = new Simulator();
simulator.playSound(new Dog());
simulator.playSound(new Cat());
}
}
package simulatorInterface;
public class Simulator {
public void playSound(Animal animal){
animal.cry();
System.out.println(animal.getAnimalName());
}
}
package simulatorInterface;
public interface Animal {
public void cry();
String getAnimalName();
}
package simulatorInterface;
public class Cat implements Animal{
String name = "cat";
public void cry(){
System.out.println("miao~");
}
public String getAnimalName(){
return name;
}
}
package simulatorInterface;
public class Dog implements Animal{
String name = "dog";
public void cry(){
System.out.println("wang!");
}
public String getAnimalName(){
return name;
}
}