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.
25 lines
814 B
25 lines
814 B
public class AnimalTest {
|
|
public static void main(String[] args) {
|
|
// 多态:使用 Animal 类型引用指向子类对象
|
|
Animal dog = new Dog();
|
|
Animal cat = new Cat();
|
|
|
|
System.out.println("===== 测试动物叫声 =====");
|
|
dog.makeSound();
|
|
cat.makeSound();
|
|
|
|
System.out.println("\n===== 测试接口能力 =====");
|
|
// 使用 instanceof 检查对象是否实现了 Swimmable 接口
|
|
if (dog instanceof Swimmable) {
|
|
((Swimmable) dog).swim();
|
|
} else {
|
|
System.out.println("这个动物不会游泳");
|
|
}
|
|
|
|
if (cat instanceof Swimmable) {
|
|
((Swimmable) cat).swim();
|
|
} else {
|
|
System.out.println("这个动物不会游泳");
|
|
}
|
|
}
|
|
}
|