# 烤地瓜: 随烧烤时间改变,地瓜的状态发生变化 0-3 生的 3-6 半生不熟 6-8 熟了 >8 烤糊了
class SweetPotato:
"""地瓜类"""
def __init__(self, num):
# 定义属性
self.cooked_state = "生的" # 地瓜的状态
self.cooked_time = 0 # 地瓜的烧烤总时间
self.num = num # 地瓜编号
self.condiments = [] # 地瓜的佐料列表
def cook(self, time):
"""烧烤"""
self.cooked_time += time # 烧烤总时间 累加每次烧烤的时间
# 判断烧烤的总时间
if self.cooked_time >= 0 and self.cooked_time < 3:
self.cooked_state = "生的"
elif self.cooked_time >=3 and self.cooked_time <6:
self.cooked_state = "半生不熟"
elif self.cooked_time >= 6 and self.cooked_time <8:
self.cooked_state = "熟了"
else:
self.cooked_state = "烤糊了"
def add_condiment(self, condiment):
"""
添加佐料
:param condiment: 佐料
"""
self.condiments.append(condiment)
def __str__(self):
return "地瓜%d 的状态: %s, 佐料:%s" % (self.num, self.cooked_state, self.condiments)
# 创建对象
digua1 = SweetPotato(1)
# 执行烧烤操作
digua1.cook(2)
digua1.add_condiment("酱油")
print(digua1)
digua1.cook(3)
digua1.add_condiment("盐")
print(digua1)
digua1.cook(1)
digua1.add_condiment("辣酱")
print(digua1)
digua1.cook(2)
print(digua1)
# 创建对象2
digua2 = SweetPotato(2)
digua2.cook(1)
digua2.add_condiment("番茄酱")
print(digua2)
digua2.cook(5)
digua2.add_condiment("沙拉")
print(digua2)
digua2.cook(4)
digua2.add_condiment("芥末")
print(digua2)
烤地瓜
最新推荐文章于 2023-09-18 01:57:05 发布