Python日常7

2018-08-06 09:14:29来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

面向对象:

class Bread:
    #定义初始化方法
    def __init__(self):
        self.cookedlevel = 0
        self.cookedstr = '生的'
        self.sauce = []#调味料
    def __str__(self):
        msg = self.cookedstr + '面包'
        if len(self.sauce) > 0:
            msg = msg + '('
            
            for temp in self.sauce:
                msg = msg + temp + ', '
            msg = msg.strip(', ')#去掉字符串尾部“, ”
            msg = msg + ')'
        return msg
    def cook(self, time):
        self.cookedlevel += time
        if self.cookedlevel > 10:
            self.cookedstr = '焦的'
        elif self.cookedlevel > 7:
            self.cookedstr = '熟的'
        elif self.cookedlevel > 4:
            self.cookedstr = '半熟'
        else:
            self.cookedstr = '生的'
    def addsauce(self, sauce):
        self.sauce.append(sauce)
        
        
bread1 = Bread()
print('一团面粉:')
print(bread1.cookedlevel)
print(bread1.cookedstr)
print(bread1.sauce)

print('做面包…………烤面包')
print('4 min 后:')
bread1.cook(4)
print(bread1)

print('6 min 后:')
bread1.addsauce('奶油')
bread1.cook(6)
print(bread1)

print('11 min 后:')
bread1.addsauce('芝麻')
bread1.cook(11)
print(bread1)

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:一、python介绍&循环

下一篇:Python_note1