食物类,动物类,饲养员类,测试类。实现饲养员给动物喂食物。
食物类: 属性name
动物类: 属性name 方法void eat
饲养员类: 属性name 方法void feed
测试类打印的结果:
张三喂大熊猫
大熊猫吃竹笋
package demo02;
public class Animal {
public String name;
public void eat(Food f){
System.out.println(name+"吃"+f.name);
}
}
package demo02;
public class Feeder {
public String name;
public void feed(Animal a){
System.out.println(name+"喂"+a.name);
}
}
package demo02;
public class Food {
public String name;
}
==========================================================
package demo02;
public class Test {
public static void main(String[] args) {
Food f = new Food();
f.name = "竹笋";
Animal a = new Animal();
a.name = "大熊猫";
Feeder fer = new Feeder();
fer.name = "张三";
fer.feed(a);
a.eat(f);
}
}