#定义一个地瓜类,注意是大驼峰命名
class SweetPoato:
#初始化,用来设置默认属性
def __init__(self):
self.cookedLevel = 0
self.cookedString = "生"
self.condiments = []
def __str__(self):
msg = "您的地瓜已经处于 " + self.cookedString + "的状态"
if len(self.condiments)>0:
msg = msg + " ,添加的佐料为:"
for temp in self.condiments:
msg = msg + temp + ", "
#把两端多余的逗号去掉
msg = msg.strip(", ")
return msg
# 用火去烤地瓜
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,temp):
self.condiments.append(temp)
#创建一个地瓜对象
digua = SweetPoato()
print(digua)
print("-----接下来开始 烤-----")
print("---烤两分钟---")
digua.cook(2)
print(digua)
print("---又烤两分钟---")
digua.cook(2)
print(digua)
print("---添加番茄酱---")
digua.addCondiments("番茄酱")
print(digua)
print("---添加芥末酱---")
digua.addCondiments("芥末酱")
print(digua)
本质
调用方法,传递数据,打印结果
网友评论