本文主要是介绍JAVA课程5 动物叫声模拟器,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
abstract class Animal//抽象类
{
abstract void Cry();//抽象方法2个
abstract void getAnimalName();
}
public class Simulator
{
void playSound(Animal animal){animal.getAnimalName();animal.Cry();}
}
public class Dog extends Animal
{void Cry(){
System.out.println("voice:WANG!WANG!WANG!");}void getAnimalName()
{
System.out.printf("Dog's");
}
}
public class Cat extends Animal
{void Cry(){
System.out.println("voice:miao!miao!miao!");}void getAnimalName()
{
System.out.printf("Cat's");
}
}
public class APPlication
{public static void main(String args[]){Simulator simulator=new Simulator();simulator.playSound(new Cat());simulator.playSound(new Dog());}}
这篇关于JAVA课程5 动物叫声模拟器的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!