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