1 #定义个类 2 class SweetPatato: 3 #初始化 4 def __init__(self): 5 self.status = '生的' 6 self.biaozhi = 0 7 self.flavour = [] 8 9 def __str__(self): 10 return '土豆%s烤了%d mina,作料有%s'%(self.status, self.biaozhi, str(self.flavour)) 11 12 def back(self,backtime): 13 14 self.biaozhi += backtime 15 16 if self.biaozhi>0 and self.biaozhi<=2: 17 self.status = '生的' 18 elif self.biaozhi>2 and self.biaozhi<=5: 19 self.status = '半生不熟' 20 elif self.biaozhi>5 and self.biaozhi<=8: 21 self.status = '熟了' 22 elif self.biaozhi>8: 23 self.status = '糊了' 24 25 def addflavour(self,item): 26 self.flavour.append(item) 27 28 tu_dou = SweetPatato() 29 print(tu_dou) 30 31 tu_dou.back(1) 32 tu_dou.back(1) 33 tu_dou.addflavour('胡椒') 34 tu_dou.back(1) 35 tu_dou.back(1) 36 print(tu_dou) 37 tu_dou.back(1) 38 tu_dou.addflavour('辣椒') 39 print(tu_dou) 40 tu_dou.back(1) 41 print(tu_dou) 42 tu_dou.addflavour('香油') 43 tu_dou.back(1) 44 tu_dou.back(1) 45 tu_dou.back(1) 46 print(tu_dou)