// 测试类 public class AnimalTest { public static void main(String[] args) { // 多态调用 Animal dog = new Dog(); Animal cat = new Cat(); dog.makeSound(); cat.makeSound(); // 判断 Dog 是否实现了 Swimmable 接口并调用 if (dog instanceof Swimmable) { ((Swimmable) dog).swim(); } // Cat 不能游泳 if (!(cat instanceof Swimmable)) { System.out.println("猫不会游泳"); } } }