用java根据提供的图片信息编写程序,要求程序包涵四个类:披萨类,培根披萨类,水果披萨类,披萨工厂类,测试类
时间: 2023-06-12 10:06:32 浏览: 90
好的,下面是Java代码实现:
披萨类:
```java
public abstract class Pizza {
protected String name;
protected String dough;
protected String sauce;
protected List<String> toppings = new ArrayList<String>();
public void prepare() {
System.out.println("Preparing " + name);
System.out.println("Tossing dough...");
System.out.println("Adding sauce...");
System.out.println("Adding toppings: ");
for (String topping : toppings) {
System.out.println(" " + topping);
}
}
public void bake() {
System.out.println("Baking " + name);
}
public void cut() {
System.out.println("Cutting " + name);
}
public void box() {
System.out.println("Boxing " + name);
}
public String getName() {
return name;
}
}
```
培根披萨类:
```java
public class BaconPizza extends Pizza {
public BaconPizza() {
name = "Bacon Pizza";
dough = "Thick crust";
sauce = "Tomato sauce";
toppings.add("Mozzarella cheese");
toppings.add("Bacon");
}
}
```
水果披萨类:
```java
public class FruitPizza extends Pizza {
public FruitPizza() {
name = "Fruit Pizza";
dough = "Thin crust";
sauce = "Vanilla sauce";
toppings.add("Mango");
toppings.add("Kiwi");
toppings.add("Strawberry");
}
}
```
披萨工厂类:
```java
public class PizzaFactory {
public Pizza createPizza(String type) {
Pizza pizza = null;
if (type.equals("bacon")) {
pizza = new BaconPizza();
} else if (type.equals("fruit")) {
pizza = new FruitPizza();
}
return pizza;
}
}
```
测试类:
```java
public class PizzaTest {
public static void main(String[] args) {
PizzaFactory factory = new PizzaFactory();
Pizza baconPizza = factory.createPizza("bacon");
baconPizza.prepare();
baconPizza.bake();
baconPizza.cut();
baconPizza.box();
System.out.println();
Pizza fruitPizza = factory.createPizza("fruit");
fruitPizza.prepare();
fruitPizza.bake();
fruitPizza.cut();
fruitPizza.box();
}
}
```
以上代码实现了根据提供的图片信息编写程序,包含了四个类:披萨类、培根披萨类、水果披萨类、披萨工厂类、测试类。程序实现了披萨的制作流程和不同种类披萨的制作方式。
阅读全文