JavaSE-12.1.2【抽象类作为形参和返回值】

package day3.yub4by01.p2;

/*
1.2 抽象类作为形参和返回值

    方法的形参是抽象类名,其实需要的是该抽象类的子类对象
    方法的返回值是抽象类名,其实返回的是该抽象类的子类对象

 */

public class AnimalDemo {

    public static void main(String[] args) {
        AnimalOperator ao = new AnimalOperator();
        Animal a = new Cat();
        ao.useAnimal(a);

        Animal a2 = ao.getAnimal(); //new Cat();
        a2.eat();
    }

}

  

package day3.yub4by01.p2;

public class AnimalOperator {

    public void useAnimal(Animal a){ //Animal a = new Cat(); 多态
        a.eat();
    }

    public Animal getAnimal(){
        Animal a = new Cat();
        return a;
    }

}

  

package day3.yub4by01.p2;

public abstract class Animal {

    public abstract void eat();

}

  

package day3.yub4by01.p2;

public class Cat extends Animal{
    @Override
    public void eat() {
        System.out.println("猫猫吃饭了吗");
    }
}

  

上一篇:Vue+element ui table 导出到excel


下一篇:Spring Boot自定义属性配置文件开启提示