package com.study.exception.demo10;
public abstract class Animal {
public abstract void eat();
}
package com.study.exception.demo10;
public class Cat extends Animal{
@Override
public void eat() {
System.out.println("猫喵喵");
}
}
package com.study.exception.demo10;
public class AnimalOperate {
public void getAnimal(Animal a){
a.eat();
}
public Animal useAnimal(){
Animal a = new Cat();
return a;
}
}
package com.study.exception.demo10;
public class AnimalDemo {
//方法的形参是抽象类名,其实需要的是该抽象类的子类对象
//方法的返回值是抽象类名,其实返回的是该抽象类的子类对象
public static void main(String[] args) {
AnimalOperate ao = new AnimalOperate();
Animal a1 = new Cat();
a1.eat();
Animal a2 = ao.useAnimal();
a2.eat();
}
}