package itheima_03;
public class Animal {
public void eat(){
System.out.println("狗吃骨头");
}
}
package itheima_03;
/*
测试类
*/
public class AnimalDemo {
public static void main(String[] args) {
//创建动物操作类的对象,调用方法
AnimalOperator ao = new AnimalOperator();
Cat c = new Cat();
ao.useAnimal(c);
Dog d = new Dog();
ao.useAnimal(d);
pig p = new pig();
ao.useAnimal(p);
}
}
package itheima_03;
/*
动物操作类
*/
public class AnimalOperator {
/*
public void useAnimal(Cat c){//cat c = new Cat();
c.eat();
}
public void usAnimal(Dog d){
d.eat();
}
public void useAnimal(Dog d) {//Dog d = new Dog();
d.eat();
}
*/
public void useAnimal(Animal a){
//Animal a = new Cat();
//Animal a = new Dog();
a.eat();
// a.lookDoor();
}
}
package itheima_03;
public class Cat extends Animal {
@Override
public void eat(){
System.out.println("猫吃鱼");
}
}
package itheima_03;
public class Dog extends Animal{
@Override
public void eat(){
System.out.println("狗吃骨头");
}
public void lookDoor(){
System.out.println("狗看门");
}
}
package itheima_03;
public class pig extends Animal{
@Override
public void eat(){
System.out.println("猪吃白菜");
}
}
多态的好处和弊端
多态的好处:提高了程序的扩张性
具体体现:定义方法的时候,使用父类型作为参数,将来在使用的时候,使用具体的子类型参与操作
多态的弊端:不能使用子类的特有功能