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.

17 lines
700 B

package w6;
public class Main {
public static void main(String[] args) {
// 测试1:Animal 引用指向 Dog(多态调用makeSound())
Animal animalDog = new Dog();
animalDog.makeSound(); // 输出:汪汪(实际执行Dog的makeSound())
// 测试2:Swimmable 引用指向 Dog(多态调用swim())
Swimmable swimmingDog = new Dog();
swimmingDog.swim(); // 输出:狗会游泳~(实际执行Dog的swim())
// 测试3:Animal 引用指向 Cat(多态调用makeSound())
Animal animalCat = new Cat();
animalCat.makeSound(); // 输出:喵喵(实际执行Cat的makeSound())
}
}