天天看点

动物声音"模拟器"

abstract class Animal {
   abstract void cry();
       abstract void getAnimaName();
 }
           
public class Simulator {
    void playSound(Animal animal){
         animal.getAnimaName();
         animal.cry();
     }
}
           
class Cat extends Animal{
    void cry(){
        System.out.println("喵喵喵");
     }
    void getAnimaName(){
        System.out.print("猫:");
     }
 }
           
class Dog extends Animal{
    void cry(){
        System.out.print("汪汪汪");
     }
    void getAnimaName(){
        System.out.print("狗:");
     }
 }
           
public class Application {
   public static void main(String args[]){
      Simulator simulator=new Simulator();
      simulator.playSound(new Cat());
      simulator.playSound(new Dog());
      }
 }
           
动物声音"模拟器"
7

继续阅读