需求分析
燒雞翅的流程分解為:燒烤和加調料
用面向對象的方式來實現(xiàn),必須定義雞翅類,有以下屬性:燒烤的時間,生熟程度,調味品。以及以下方法:燒烤,添加調料
燒烤時間與生熟時間的關聯(lián):
- 0-3分鐘:生的
- 3-5分鐘:半生不熟
- 5-8分鐘:熟了
- 大于8分鐘:焦了

燒雞翅
代碼實現(xiàn)如下:
class Wing():
"""燒雞翅"""
def __init__(self):
print('----燒雞翅,我最中意食-----')
self.cookedLevel = 0 # 燒雞翅的時間
self.cookedString = '生的' # 生熟程度
self.condiments = [] # 調味
print('剛買的雞翅是生的,沒有調料')
def cook(self, time):
self.cookedLevel += time
if self.cookedLevel > 8:
self.cookedString = '燒焦了'
elif self.cookedLevel > 5:
self.cookedString = '燒好了'
elif self.cookedLevel > 3:
self.cookedString = '半生不熟'
else:
self.cookedString = '生的'
def addCondiments(self, condiments):
print('---添加調料---')
self.condiments.append(condiments)
def __str__(self):
print('----燒了%s分鐘---' % self.cookedLevel)
msg = '雞翅的生熟度:' + self.cookedString
if len(self.condiments) > 0:
msg = msg + "\n調料有:"
for temp in self.condiments:
msg = msg + temp + ', '
msg = msg.rstrip(', ')
return msg
wing = Wing()
print('----開始燒雞翅----')
wing.cook(4)
print(wing)
wing.addCondiments('醬油')
wing.cook(3)
print(wing)
wing.addCondiments('芝麻')
wing.cook(1)
print(wing)
wing.cook(2)
print(wing)
結果:
----燒雞翅,我最中意食-----
剛買的雞翅是生的,沒有調料
----開始燒雞翅----
----燒了4分鐘---
雞翅的生熟度:半生不熟
---添加調料---
----燒了7分鐘---
雞翅的生熟度:燒好了
調料有:醬油
---添加調料---
----燒了8分鐘---
雞翅的生熟度:燒好了
調料有:醬油, 芝麻
----燒了10分鐘---
雞翅的生熟度:燒焦了
調料有:醬油, 芝麻