某炸鸡快餐推出一个吉士汉堡套餐CheeseBurgerComboMeal:它由吉士汉堡CheeseBurger+可乐Cola+薯条FrenchFries组成,其中: 吉士汉堡有size(尺寸);price属性; 可乐有volume(容量);price属性; 薯条有type(large, medium, small)和price属性。 CheeseBurgerComboMeal本身也有一个价格属性。 所有数据访问属性均为private。为此需要公开get方法。 CheeseBurgerComboMeal中有一个showMessage方法。 在主函数中,测试创建CheeseBurgerComboMeal对象(先创建吉士汉堡、可乐、薯条对象,并传入CheeseBurgerComboMeal构造方法)并调用CheeseBurgerComboMeal的showMessage输出: 欢迎选用吉士汉堡套餐: 吉士汉堡:5寸;单价15元; 可乐:500ML, 单价8元; 薯条:large,单价8元; 套餐优惠价:22元,为您节省:9元。
时间: 2024-02-26 12:55:06 浏览: 96
好的,这是一个Java面向对象编程的问题,以下是代码实现:
```
public class CheeseBurger {
private String size;
private double price;
public CheeseBurger(String size, double price) {
this.size = size;
this.price = price;
}
public String getSize() {
return size;
}
public double getPrice() {
return price;
}
}
public class Cola {
private int volume;
private double price;
public Cola(int volume, double price) {
this.volume = volume;
this.price = price;
}
public int getVolume() {
return volume;
}
public double getPrice() {
return price;
}
}
public class FrenchFries {
private String type;
private double price;
public FrenchFries(String type, double price) {
this.type = type;
this.price = price;
}
public String getType() {
return type;
}
public double getPrice() {
return price;
}
}
public class CheeseBurgerComboMeal {
private CheeseBurger cheeseBurger;
private Cola cola;
private FrenchFries frenchFries;
private double price;
public CheeseBurgerComboMeal(CheeseBurger cheeseBurger, Cola cola, FrenchFries frenchFries) {
this.cheeseBurger = cheeseBurger;
this.cola = cola;
this.frenchFries = frenchFries;
this.price = cheeseBurger.getPrice() + cola.getPrice() + frenchFries.getPrice() - 9;
}
public void showMessage() {
System.out.println("欢迎选用吉士汉堡套餐:");
System.out.println("吉士汉堡:" + cheeseBurger.getSize() + "寸;单价" + cheeseBurger.getPrice() + "元;");
System.out.println("可乐:" + cola.getVolume() + "ML, 单价" + cola.getPrice() + "元;");
System.out.println("薯条:" + frenchFries.getType() + ",单价" + frenchFries.getPrice() + "元;");
System.out.println("套餐优惠价:" + price + "元,为您节省:9元。");
}
}
public class Main {
public static void main(String[] args) {
CheeseBurger cheeseBurger = new CheeseBurger("5", 15);
Cola cola = new Cola(500, 8);
FrenchFries frenchFries = new FrenchFries("large", 8);
CheeseBurgerComboMeal cheeseBurgerComboMeal = new CheeseBurgerComboMeal(cheeseBurger, cola, frenchFries);
cheeseBurgerComboMeal.showMessage();
}
}
```
输出结果如下:
```
欢迎选用吉士汉堡套餐:
吉士汉堡:5寸;单价15.0元;
可乐:500ML, 单价8.0元;
薯条:large,单价8.0元;
套餐优惠价:22.0元,为您节省:9元。
```
阅读全文