烤地瓜综合应用python
时间: 2023-12-07 10:39:06 浏览: 141
烤地瓜是一个很好的面向对象的综合应用,以下是一个简单的烤地瓜类的实现,包括被烤的时间和对应的地瓜状态以及添加调料的功能:
```python
class SweetPotato:
def __init__(self):
self.cookedLevel = 0
self.cookedString = "生的"
self.condiments = []
def __str__(self):
return "地瓜状态:%s(%d), 添加的调料有:%s" % (self.cookedString, self.cookedLevel, str(self.condiments))
def cook(self, time):
self.cookedLevel += time
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 = "熟了"
else:
self.cookedString = "烤糊了"
def addCondiments(self, condiment):
self.condiments.append(condiment)
```
使用方法如下:
```python
sweetPotato = SweetPotato()
print(sweetPotato) # 输出:地瓜状态:生的(0), 添加的调料有:[]
sweetPotato.cook(2)
print(sweetPotato) # 输出:地瓜状态:生的(2), 添加的调料有:[]
sweetPotato.cook(2)
print(sweetPotato) # 输出:地瓜状态:半生不熟(4), 添加的调料有:[]
sweetPotato.addCondiments("盐")
sweetPotato.addCondiments("孜然")
print(sweetPotato) # 输出:地瓜状态:半生不熟(4), 添加的调料有:['盐', '孜然']
sweetPotato.cook(3)
print(sweetPotato) # 输出:地瓜状态:熟了(7), 添加的调料有:['盐', '孜然']
```
阅读全文