1 使用父类作为方法形参实现多态
feet(Animal animal)
2 使用父类作为方法返回值实现多态
public Animal sellPet(int choice) {
Animal a=null;
switch (choice) {
case 1:
a=new Duck(“小黄鸭”);
break;
case 2:
a=new Dog(“大黄”);
break;
case 3:
a=new Cat(“花花”);
break;
}
return a;
}
}
测试
PetFactory pf=new PetFactory();
Animal a=pf.sellPet(2);
System.out.println(a.name);
3 可以使用父类做其他类的属性
public class People{
String name;// 姓名
Animal a;
}