执行结果:
烧开水的例子:
class Boiled_water: def __init__(self): self.waterstring = '冷水' self.waterlevel = 0 def __str__(self): #水的状态描述 return "烧了%s分钟水的状态是:%s"%(self.waterlevel,self.waterstring) def Boil(self,boil_tiom): self.waterlevel += boil_tiom if self.waterlevel >= 0 and self.waterlevel <= 3: self.waterstring = '冷水' elif self.waterlevel > 3 and self.waterlevel < 10: self.waterstring = '温水' elif self.waterlevel >= 10 and self.waterlevel <15: self.waterstring = '热水' elif self.waterlevel >= 15 and self.waterlevel <30: self.waterstring = '沸水' else: self.waterstring = '水烧干了' water = Boiled_water() water.Boil(2) print(water) water.Boil(4) print(water) water.Boil(8) print(water) water.Boil(10) print(water) water.Boil(12) print(water)运行结果:
转载于:https://www.cnblogs.com/shuimohei/p/10349375.html