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.

28 lines
782 B

package w6;
public class AnimalTest {
public static void main(String[] args) {
//多态调用Animal抽象类
System.out.println("===动物叫声测试===");
Animal dog1=new Dog();
Animal cat1=new Cat();
dog1.makeSound();
cat1.makeSound();
//多态调用Swimmable接口
System.out.println("\n===游泳能力测试===");
Swimmable swimmer = new Dog();
swimmer.swim();
//数组遍历多态演示
System.out.println("\n===数组多态遍历===");
Animal[] animals={new Dog(),new Cat()};
for (Animal animal :animals) {
animal.makeSound();
if (animal instanceof Dog) {
((Dog) animal).swim();
}
}
}
}