实例属性:price(pizza价格), 实例方法:show()—显示‘普通pizza制作完成,价格:XX元,请慢用! ’ 2,创建SeaFoodPizza类,表示海鲜pizza 继承pizza类,新增实例属性seafood(配料) 实例方法show():—显示‘海鲜pizza制作完成,材料为:XX,价格:XX元,请慢用!’ 3,创建PizzaManagement类,表示管理pizza生产 类属性: base_price ,默认为20元 实例方法create_Pizza(self): 利用pizza类实例化一个普通pizza 实例方法create create_SeaFoodPizza(self,seafood_type): seafood_type 代表海鲜类型,固定2种属性值: 1:龙虾(基础价格+50元),2:鲍鱼(基础价格+100) 根据传入的参数生产对应的海鲜pizza
时间: 2023-06-14 09:05:06 浏览: 75
JSF实例学习--比萨(pizza)订购系统
5星 · 资源好评率100%
下面是代码实现:
```python
# 定义Pizza类
class Pizza:
def __init__(self, price):
self.price = price
def show(self):
print(f"普通pizza制作完成,价格:{self.price}元,请慢用!")
# 定义SeaFoodPizza类,继承自Pizza类
class SeaFoodPizza(Pizza):
def __init__(self, price, seafood):
super().__init__(price)
self.seafood = seafood
def show(self):
print(f"海鲜pizza制作完成,材料为:{self.seafood},价格:{self.price}元,请慢用!")
# 定义PizzaManagement类
class PizzaManagement:
base_price = 20 # 类属性,基础价格为20元
def create_Pizza(self):
pizza = Pizza(self.base_price)
return pizza
def create_SeaFoodPizza(self, seafood_type):
if seafood_type == 1:
price = self.base_price + 50
seafood = "龙虾"
elif seafood_type == 2:
price = self.base_price + 100
seafood = "鲍鱼"
else:
print("无效的海鲜类型!")
return None
sea_food_pizza = SeaFoodPizza(price, seafood)
return sea_food_pizza
```
使用示例:
```python
# 创建PizzaManagement实例
pm = PizzaManagement()
# 创建普通pizza
pizza = pm.create_Pizza()
pizza.show()
# 创建海鲜pizza
sea_food_pizza = pm.create_SeaFoodPizza(1)
sea_food_pizza.show()
```
阅读全文