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