package com.ithema_46;
public abstract class Animal {
public abstract void eat();
}
package com.ithema_46;
/*
测试类
方法的形参是抽象类名,其实需要的是该抽象类的子类对象
方法的返回值是抽象类名,其实返回的是该抽象类的子类对象
*/
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 com.ithema_46;
public class AnimalOperator {
public void useAnimal(Animal a){ //Animal a = new Cat();
a.eat();
}
public Animal getAnimal(){
Animal a = new Cat();
return a;
}
}
package com.ithema_46;
public class Cat extends Animal{
public void eat() {
System.out.println("猫吃鱼");
}
}