多态的问题
public class Demo {
public static void main(String[] args){
Master ma = new Master();
ma.feed(new Animal(), new Food());
ma.feed(new Cat(), new Bone());
ma.feed(new Dog(), new Fish());
}
}
class Animal{
public void eat(Food f){
System.out.println("我是一个小动物,正在吃" + f.getFood());
}
}
class Cat extends Animal{
public void eat(Food f){
System.out.println("我是一只小猫咪,正在吃" + f.getFood());
}
}
class Dog extends Animal{
public void eat(Food f){
System.out.println("我是一只狗狗,正在吃" + f.getFood());
}
}
class Food{
public String getFood(){
return "事物";
}
}
class Fish extends Food{
public String getFood(){
return "鱼";
}
}
class Bone extends Food{
public String getFood(){
return "骨头";
}
}
class Master{
public void feed(Animal an, Food f){
an.eat(f);
}
}