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.
 
 
 

21 lines
517 B

// 测试类
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("猫不会游泳");
}
}
}