Java 动物声音模拟器

package Animal;

public class Application {

	/**
	 * @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 Animal;

public abstract class Animal {//未使用接口
	public abstract void cry();
		public abstract String getAnimalName();
}
package Animal2

public interface Animal {//使用接口
	public abstract void cry();
	public abstract String getAnimalName();
}


package Animal;

public class cat extends Animal{
	public void cry(){
		System.out.println("喵喵...喵喵");
	}
	public String getAnimalName(){
		return "猫";
	}
}
package Animal;

public class Simulator {
	public void playsound(Animal animal){
		System.out.print("现在播放"+animal.getAnimalName()+"类声音:");
		animal.cry();
	}
}

package Animal;

public class dog extends Animal {
	public void cry(){
		System.out.println("汪汪...汪汪");
	}
	public String getAnimalName(){
		return "狗";
	}
}

Java 动物声音模拟器_第1张图片

你可能感兴趣的:(Java 动物声音模拟器)