public class HelloWorld {
public static void main(String[] args) {
FastFood food =
new
FriedRice();
System.out.println(food.getDesc() +
" "
+ food.cost() +
"元"
);
System.out.println(
"========"
);
FastFood food1 =
new
FriedRice();
food1 =
new
Egg(food1);
System.out.println(food1.getDesc() +
" "
+ food1.cost() +
"元"
);
System.out.println(
"========"
);
FastFood food2 =
new
FriedNoodles();
food2 =
new
Bacon(food2);
System.out.println(food2.getDesc() +
" "
+ food2.cost() +
"元"
);
}
}
abstract class FastFood {
private float price;
private String desc;
public FastFood() {}
public FastFood(float price, String desc) {
this
.price = price;
this
.desc = desc;
}
public float getPrice() {
return
price;
}
public void setPrice(float price) {
this
.price = price;
}
public String getDesc() {
return
desc;
}
public void setDesc(String desc) {
this
.desc = desc;
}
public abstract float cost();
}
class FriedRice extends FastFood {
public FriedRice() {
super
(10,
"炒饭"
);
}
@Override
public float cost() {
return
getPrice();
}
}
class FriedNoodles extends FastFood {
public FriedNoodles() {
super
(12,
"炒面"
);
}
@Override
public float cost() {
return
getPrice();
}
}
abstract class Garnish extends FastFood {
private FastFood fastFood;
public FastFood getFastFood() {
return
fastFood;
}
public void setFastFood(FastFood fastFood) {
this
.fastFood = fastFood;
}
public Garnish(FastFood fastFood, float price, String desc) {
super
(price, desc);
this
.fastFood = fastFood;
}
}
class Egg extends Garnish {
public Egg(FastFood fastFood) {
super
(fastFood, 1,
"鸡蛋"
);
}
@Override
public float cost() {
return
getPrice() + getFastFood().getPrice();
}
@Override
public String getDesc() {
return
super
.getDesc() + getFastFood().getDesc();
}
}
class Bacon extends Garnish {
public Bacon(FastFood fastFood) {
super
(fastFood,2,
"培根"
);
}
@Override
public float cost() {
return
getPrice() + getFastFood().getPrice();
}
@Override
public String getDesc() {
return
super
.getDesc() + getFastFood().getDesc();
}
}