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.

23 lines
721 B

public class AnimalTest {
public static void main(String[] args) {
// 多态方式创建对象
Animal dog = new Dog();
Animal cat = new Cat();
// 调用 makeSound 方法(多态体现)
System.out.println("=== 动物叫声测试 ===");
dog.makeSound();
cat.makeSound();
// 测试 Swimable 接口(Dog 能游泳,Cat 不能)
System.out.println("\n=== 游泳能力测试 ===");
if (dog instanceof Swimmable) {
((Swimmable) dog).swim();
}
if (cat instanceof Swimmable) {
((Swimmable) cat).swim();
} else {
System.out.println("小猫不会游泳哦!");
}
}
}