public?class?Run?{
public?static?void?main(String[]?args)?{
Master?master?=? new?Master();
master.feedDog("닭 뼈");
master.feedCat("닭 뼈");
} p> p>
}
class?Master?{
private?Pet?mPet;
private?Food?mFood;
public?void?feedCat(String?food)?{
mPet?=?new?Cat();
mFood?=?new?Food(food);
p>mPet.eat(mFood);
}
public?void?feedDog(String?food)?{
mPet?=? new?Dog();
mFood?=?new?Food(food);
mPet.eat(mFood);
}
}
class?Dog?extends?Pet{
@Override
public?void?eat(음식?음식)? {
System.out.println("강아지 먹이주기"+food.getFood());
if?(food.getFood().matches(Food.BONE)) ?{
System.out.println("강아지가 먹고 있습니다"+food.getFood()+"!");
}else?{
System .out.println("근데 강아지가 먹기를 싫어해요"+food.getFood()+"!");
}
}
}
class?Cat?extends?Pet{
@Override
public?void?eat(Food?food)?{
시스템 .out.println("고양이에게 먹이 주기"+food.getFood());
if?(food.getFood().matches(Food.FISH))?{
p>
System.out.println("새끼 고양이가 "+food.getFood()+"를 먹고 있습니다! ");
}else?{
System.out.println("하지만 새끼 고양이는 먹는 것을 좋아하지 않습니다"+food.getFood()+"!");
}
}
}
class?음식?{
public?final?static?String ?BONE? =?".*bone.*";
public?final?static?String?FISH?=?".*fish.*";
private?String ?food;
public?String?getFood()?{
return?food;
}
public?void?setFood (String? food)?{
this.food?=?food;
}
public?Food(String?food)?{
this.food?=?food;
}
}
class?Pet?{
public?void ?먹어요( 음식?음식)?{
}
}