对象14

 1 #!/usr/bin/python
 2 # -*- coding: UTF-8 -*-
 3 
 4 class HotDog:
 5     def __init__(self):
 6         self.cooked_level=0
 7         self.cooked_string=""
 8         self.condiments=[]
 9 
10     def __str__(self):
11         msg="热-狗"
12         if len(self.condiments)>0:          # len() 方法返回对象(字符、列表、元组等)长度或项目个数
13             msg=msg+"添加"
14         for i in self.condiments:           # 循环字符串
15             msg=msg + i + ", "              # i = 得到字符,"番茄酱" "芥末酱"
16         msg=msg.strip(", ")                 # strip() 方法用于移除字符串头尾指定的字符(默认为空格)  消除字符尾","
17         msg=self.cooked_string+msg+","
18         return msg
19 
20     def cook(self,time):
21         self.cooked_level=self.cooked_level+time
22         if self.cooked_level>8:
23             self.cooked_string="烤焦的"
24         elif self.cooked_level>5:
25             self.cooked_string="刚刚好"
26         elif self.cooked_level>3:
27             self.cooked_string="半生的"
28         else:
29             self.cooked_string="生的"
30     def addCondiment(self,condiment):
31         self.condiments.append(condiment)       # append() 方法用于在列表末尾添加新的对象。
32 
33 myDog=HotDog()
34 # print myDog
35 # print "***烤热狗4分钟后..."
36 # myDog.cook(4)
37 # print myDog
38 # print "再烤热狗3分钟..."
39 # myDog.cook(3)
40 # print myDog
41 print "如果继续烤10分钟会发生什么?"
42 myDog.cook(10)
43 print myDog
44 print "现在我要在热狗上加一些调料"
45 myDog.addCondiment("番茄酱")
46 myDog.addCondiment("芥末酱")
47 print myDog

 

posted on 2018-01-26 14:40  新手爱好者  阅读(104)  评论(0编辑  收藏  举报

导航