1 # 面向对象分析烤地瓜
 2 
 3 # 1.抽象类
 4 # 1.1人类
 5 # 1.2地瓜类
 6 
 7 
 8 # 分析人类里面的属性和方法
 9 # 属性:名字,性别,烤龄
10 # 方法:烤地瓜的方法,添加佐料
11 
12 # 地瓜类中的属性和方法
13 # 属性:烤地瓜的状态,烤地瓜的时间,佐料列表
14 # 方法:无
15 
16 
17 class Person(object):
18     def __init__(self,name,sex,roast_age):
19         self.name=name
20         self.sex=sex
21         self.roast_age=roast_age
22     # 烤地瓜的方法
23     def roast(self,time,current_sweet_potato):
24         #修改烤地瓜的总时间
25         current_sweet_potato.roast_time+=time
26         # 根据烤地瓜的时间,来设置烤地瓜的状态
27         if current_sweet_potato.roast_time > 10:
28             # 修改烤地瓜的状态
29             current_sweet_potato.roast_status = "烤糊了"
30         elif current_sweet_potato.roast_time > 8:  # 时间范围[9-10]
31             # 修改烤地瓜的状态
32             current_sweet_potato.roast_status = "烤好了"
33         elif current_sweet_potato.roast_time > 5:  # 时间范围[6-8]
34             # 修改烤地瓜的状态
35             current_sweet_potato.roast_status = "半生不熟"
36         else:
37             # 修改烤地瓜的状态
38             current_sweet_potato.roast_status = "生的"
39     def add_condiment(self,condiment,current_sweet_potato):
40         # 把佐料添加到佐料列表
41         current_sweet_potato.condiment_list.append(condiment)
42 
43     # 查看地瓜状态
44     # def show_sweet_potato(self, current_sweet_potato):
45     #     print(current_sweet_potato)
46     def __str__(self):
47         return "姓名: %s 性别:%s 烤龄: %d " % (self.name, self.sex,self.roast_age)
48 
49 
50 class SweetPotato(object):
51     def __init__(self):
52         # 烤地瓜的状态
53         self.roast_status = "生的"
54         # 烤地瓜的时间
55         self.roast_time = 0
56         # 佐料列表属性--> 存储添加的佐料的
57         self.condiment_list = list()
58 
59     def __str__(self):
60         if self.condiment_list:
61             # 代码执行到此说明,佐料列表里面有佐料信息
62             # 烤好了地瓜[番茄酱,孜然]
63             # 把佐料列表转成字符串
64             # msg = self.roast_status + "地瓜" + str(self.condiment_list)
65             # 使用字符串利用字符串拼接的操作,把列表转成字符串
66             condiment_str = ",".join(self.condiment_list)
67             # print(condiment_str, type(condiment_str))
68             msg = self.roast_status + "地瓜" + "[" + condiment_str + "]"
69             return msg
70         else:
71             # 没有佐料
72             return self.roast_status + "地瓜"
73 
74 print("============准备一个地瓜==========")
75 sweet_potato = SweetPotato()
76 print(sweet_potato)
77 print("============准备找一个烤地瓜的师傅==============")
78 person = Person("老王","",20 )
79 print(person)
80 print("============先烤三分钟==========")
81 person.roast(3, sweet_potato)
82 print(sweet_potato)
83 print("============再烤三分钟==========")
84 person.roast(3, sweet_potato)
85 print(sweet_potato)
86 print("============再烤三分钟==========")
87 person.roast(3, sweet_potato)
88 print(sweet_potato)
89 print("============添加佐料==========")
90 person.add_condiment("番茄酱", sweet_potato)
91 person.add_condiment("孜然", sweet_potato)
92 print(sweet_potato)

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。