烤地瓜

来源:互联网 发布:英语关键词软件 编辑:程序博客网 时间:2024/04/27 13:45
    # 烤地瓜:  随烧烤时间改变,地瓜的状态发生变化  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)# 创建对象2digua2 = 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)
原创粉丝点击