[root@localhost 05-class]# cat cook_sweet_potato.py ''' 思考: 1.定義一個地瓜類 2.有什麼方法,什麼屬性 ''' #1.定義一個地瓜類 class cook_sweet_potato(): def __init__(self): self.cooked_string = "生的" self.cooked_level = 0 self.condiments = [] #爲了可以存儲多個數據,每每在開發中讓一個屬性是列表 def __str__(self): return "地瓜 狀態:%s(%d),添加的佐料有:%s"%(self.cooked_string,self.cooked_level,str(self.condiments)) def cook(self,cooked_time): self.cooked_level += cooked_time if self.cooked_level >= 0 and self.cooked_level<3: self.cooked_string = "生的" elif self.cooked_level >=3 and self.cooked_level<5: self.cooked_string = "半生不熟" elif self.cooked_level >=5 and self.cooked_level<=8: self.cooked_string = "熟了" elif self.cooked_level >8: self.cooked_string = "烤糊了" def add_condiments(self,item): #由於item這個變量指向了一個佐料,全部接下來須要將item放到append裏面 self.condiments.append(item) #2.建立了一個地瓜對象 sweetPotato = cook_sweet_potato() #print(sweetPotato) #3.開始烤地瓜 cooked_time = int(input("請輸入你要烤地瓜的時間:")) sweetPotato.cook(cooked_time) sweetPotato.add_condiments("孜然") sweetPotato.add_condiments("辣椒") sweetPotato.add_condiments("芥末") print(sweetPotato)