package cn.campsg.java.experiment.entity;
public class Feeder {
public Tiger[] tiger=new Tiger[3];
public Feeder() {
for(int i=0;i<tiger.length;i++){
tiger[i]=new Tiger();
}
tiger[0].setType("华南虎");
tiger[1].setType("东北虎");
tiger[2].setType("新疆虎");
}
public void feed(String type){
if(type!=null){
for(int i=0;i<tiger.length;i++){
if(tiger[i].getType()==type){
tiger[i].eat();
}
}
}
}
public void feed(){
feed("华南虎");
feed("东北虎");
feed("新疆虎");
}
}
package cn.campsg.java.experiment.entity;
public class Tiger {
private String type;
public Tiger(String type) {
this.type = type;
}
public Tiger(){
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public void eat(){
String stuff="0";
if(this.type=="华南虎")
stuff="牛肉";
if(this.type=="东北虎")
stuff="猪肉";
if(this.type=="新疆虎")
stuff="羊肉";
System.out.println( "饲养员喂" + type + "吃" + stuff);
}
}
package cn.campsg.java.experiment;
import cn.campsg.java.experiment.entity.Feeder;
public class MainClass {
public static void main(String[] args) {
Feeder feeder=new Feeder();
feeder.feed();
feeder.feed("华南虎");
}
}