多态实现主人喂养宠物

/*
多态实现主人喂养宠物
 */
package day_7;

public class Pet {
    public void eat(){
        System.out.println("***********宠物在吃**********");
    }
}
package day_7;

public class Dog extends Pet {
    public void eat(){
        System.out.println("**********狗在吃骨头**********");
    }
}
package day_7;

public class Cat extends Pet {
    public void eat(){
        System.out.println("**********猫在吃鱼**********");
    }
}
package day_7;

public class Bird extends Pet {
    public void eat(){
        System.out.println("**********鸟在吃虫子**********");
    }
}
package day_7;

public class Master {
    public void feed(Pet pet){   //----->(形参:父类  =  实参:子类)----->实现多态
        pet.eat();   //优先调用子类重写方法
    }
}
package day_7;
public class test {
    public static void main(String[] args) {
        Dog dog=new Dog();
        Cat cat=new Cat();
        Bird bird=new Bird();
        Master master=new Master();
        master.feed(dog);
        master.feed(cat);
        master.feed(bird);
    }
}

  

上一篇:004-猫狗队列


下一篇:SpringCloud Feign使用详解