#定义一个烤地瓜的类,烤地瓜时间可叠加,作料可叠加 class kaodigua: #初始化烤地瓜的一些属性 def __init__(self): self.cookedString = "生的" self.cookedLevel = 0 self.zuoliao =[] #储存地瓜的作料添加类别 #实例化返回参数 def __str__(self): return "现在地瓜状态是%s,加的作料有%s"%(self.cookedString,self.zuoliao) #定义烤地瓜的方法 def cook(self,cookedTime): self.cookedLevel += cookedTime if self.cookedLevel >=0 and self.cookedLevel < 3: self.cookedString = "生的" elif self.cookedLevel >= 3 and self.cookedLevel < 5: self.cookedString = "本生不熟的" elif self.cookedLevel >= 5 and self.cookedLevel < 8: self.cookedString = "熟的" elif self.cookedLevel >=8: self.cookedString = "烤糊了" #定义添加作料的方法 def jia_zuo_liao(self,zuoliao): self.zuoliao.append(zuoliao) #创建一个烤地瓜对象 digua = kaodigua() digua.cook(1) digua.jia_zuo_liao("盐") print(digua) digua.cook(3) digua.jia_zuo_liao("芥末") print(digua) digua.cook(5) digua.jia_zuo_liao("白砂糖") print(digua)