用Java编写一个程序,实现模拟开披萨店 定义一个抽象类,Pizza,包括成员变量,名字,口味,辅料,实现构造函数,实现制作Pizza的步骤,包括准备,和面,烘烤,切片,后两个是抽象方法。 定义一个抽象类,PizzaStore,实现定披萨的方法orderPizza,传入Pizza的名字,返回Pizza,定义一个抽象方法,createPizza,在orderPizza中调用,实现不可以预定并制作不同口味的pizza。 分别定义两个Pizza的子类继承Pizza,并分别实现抽象方法 定义一个PizzaStore的子类继承PizzaStore,实现抽象方法 主类:生成两款Pizza,和一个PizzaStore店对象,实现购买Pizza
时间: 2023-07-15 15:13:14 浏览: 169
好的,以下是代码实现:
抽象类 Pizza:
```java
public abstract class Pizza {
protected String name;
protected String flavor;
protected String[] toppings;
public Pizza(String name, String flavor, String[] toppings) {
this.name = name;
this.flavor = flavor;
this.toppings = toppings;
}
public void prepare() {
System.out.println("Preparing " + name);
System.out.println("Adding toppings:");
for (String topping : toppings) {
System.out.println("- " + topping);
}
}
public abstract void bake();
public abstract void cut();
public void box() {
System.out.println("Putting pizza in box");
}
}
```
抽象类 PizzaStore:
```java
public abstract class PizzaStore {
public Pizza orderPizza(String name) {
Pizza pizza = createPizza(name);
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
return pizza;
}
protected abstract Pizza createPizza(String name);
}
```
PizzaStore 的子类 PizzaStoreBJ:
```java
public class PizzaStoreBJ extends PizzaStore {
@Override
protected Pizza createPizza(String name) {
if (name.equals("cheese")) {
return new CheesePizzaBJ();
} else if (name.equals("pepperoni")) {
return new PepperoniPizzaBJ();
} else {
throw new IllegalArgumentException("Invalid pizza name: " + name);
}
}
}
```
Pizza 的子类 CheesePizza:
```java
public class CheesePizza extends Pizza {
public CheesePizza() {
super("Cheese Pizza", "cheese", new String[]{"mozzarella cheese", "tomato sauce"});
}
@Override
public void bake() {
System.out.println("Baking for 20 minutes at 180 degrees Celsius");
}
@Override
public void cut() {
System.out.println("Cutting pizza into triangles");
}
}
```
Pizza 的子类 PepperoniPizza:
```java
public class PepperoniPizza extends Pizza {
public PepperoniPizza() {
super("Pepperoni Pizza", "pepperoni", new String[]{"mozzarella cheese", "tomato sauce", "pepperoni"});
}
@Override
public void bake() {
System.out.println("Baking for 25 minutes at 200 degrees Celsius");
}
@Override
public void cut() {
System.out.println("Cutting pizza into squares");
}
}
```
PizzaStoreBJ 中使用:
```java
public class Main {
public static void main(String[] args) {
PizzaStoreBJ store = new PizzaStoreBJ();
Pizza pizza1 = store.orderPizza("cheese");
System.out.println("Ordered a " + pizza1.name + "\n");
Pizza pizza2 = store.orderPizza("pepperoni");
System.out.println("Ordered a " + pizza2.name + "\n");
}
}
```
输出结果:
```
Preparing Cheese Pizza
Adding toppings:
- mozzarella cheese
- tomato sauce
Baking for 20 minutes at 180 degrees Celsius
Cutting pizza into triangles
Putting pizza in box
Ordered a Cheese Pizza
Preparing Pepperoni Pizza
Adding toppings:
- mozzarella cheese
- tomato sauce
- pepperoni
Baking for 25 minutes at 200 degrees Celsius
Cutting pizza into squares
Putting pizza in box
Ordered a Pepperoni Pizza
```
阅读全文