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
824 B
25 lines
824 B
public class Main {
|
|
public static void main(String[] args) {
|
|
// 测试多态
|
|
Animal animal1 = new Dog();
|
|
Animal animal2 = new Cat();
|
|
|
|
System.out.println("Testing polymorphism:");
|
|
animal1.makeSound(); // 应该输出 Woof!
|
|
animal2.makeSound(); // 应该输出 Meow!
|
|
|
|
// 测试 Dog 的 swim 方法
|
|
System.out.println("\nTesting Dog's swim method:");
|
|
if (animal1 instanceof Swimmable) {
|
|
((Swimmable) animal1).swim();
|
|
}
|
|
|
|
// 测试 Cat 是否实现了 Swimmable
|
|
System.out.println("\nTesting if Cat is Swimmable:");
|
|
if (animal2 instanceof Swimmable) {
|
|
((Swimmable) animal2).swim();
|
|
} else {
|
|
System.out.println("Cat is not Swimmable.");
|
|
}
|
|
}
|
|
}
|