package miniqi;
public abstract class Animal{
abstract void cry();
abstract String getAnimaName();
}
package miniqi;
public class Cat extends Animal{
void cry() {
System.out.println("动物的叫声是喵喵");
}
String getAnimaName() {
return "猫";
}
}
package miniqi;
public class Dog extends Animal{
void cry() {
System.out.println("动物的叫声是汪汪");
}
String getAnimaName() {
return "狗";
}
}
package miniqi;
public class Simulator {
void playSound(Animal animal) {
if(animal instanceof Dog) {
animal.cry();
System.out.println("动物的品种为"+animal.getAnimaName());}
else if(animal instanceof Cat){
animal.cry();
System.out.println("动物的品种为"+animal.getAnimaName());
}
}
}
package miniqi;
public class Application {
public static void main(String[] args) {
// TODO Auto-generated method stub
Simulator simulator = new Simulator();
simulator.playSound(new Dog());
simulator.playSound(new Cat());
}
}
动物声音模拟器
最新推荐文章于 2024-07-01 01:00:00 发布