You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

30 lines
839 B

public class Main {
public static void main(String[] args) {
Animal dog = new Dog("旺财");
Animal cat = new Cat("咪咪");
System.out.println("=== 动物叫声测试 ===");
dog.makeSound();
cat.makeSound();
System.out.println("\n=== 多态调用测试 ===");
testAnimal(dog);
testAnimal(cat);
System.out.println("\n=== 游泳能力测试 ===");
if (dog instanceof Swimmable) {
((Swimmable) dog).swim();
}
if (cat instanceof Swimmable) {
((Swimmable) cat).swim();
} else {
System.out.println(cat.getName() + "不会游泳");
}
}
public static void testAnimal(Animal animal) {
System.out.print(animal.getName() + "的叫声:");
animal.makeSound();
}
}