【发布时间】:2020-07-25 23:10:14
【问题描述】:
我正在学习使用 OOP 开发代码。但是,我在理解何时使用 __init__ 构造函数时遇到问题。 __init__ 在 OOP 中是强制性的吗?如果是这样,我将如何使用__init__?
以下代码的作用是获取用户请求的比萨饼大小和配料以及退货和最终总数。
当我运行以下代码时:
class Pizza:
""" customer orders pizza size and pizza toppings"""
def size_menu(self): # Provides user a menu
self.menu_s = """
What size pizza would you like?
_____________________________________________________________
| 1: Small | 2: Large | 3: Extra Large | 4: Party Size |
| $6.23 | $10.23 | $12.23 | $24.23 |
|___________|____________|__________________|________________|
"""
print(self.menu_s)
return self.menu_s
def size_order(self): # Gets size wanted and returns pizza total.
size_mappings = {
1: "Small",
2: "Large",
3: "Extra Large",
4: "Party Size"
}
cost_mappings = {
"Small": 6.23,
"Large": 10.23,
"Extra Large": 12.23,
"Party Size": 24.23
}
response = input('-') # user inters 1-4 for pizza size wanted and returns a size total.
self.size_wanted = float(response) # Turns response as a float
self.size_wanted = size_mappings[self.size_wanted] # Size requested
self.size_cost = cost_mappings[self.size_wanted] # Cost of size
print(f"Getting your {self.size_wanted} pizza ready.")
print(f"Your current total is: ${self.size_cost}")
return self.size_cost
def topping_menu(self): # Provides user with toppings menu
self.menu_t = """
What toppings do you want on your pizza?
_____________________________________________________
| 1:Bacon | 4:Anchovies | 7:Black Olives |
| 2:Pepperoni | 5:Spinach | 8:Chicken |
| 3:Mushrooms | 6:Onions | 9:Ground Beef |
|________________|__________________|_________________|
What toppings do you want on your pizza?
"""
print(self.menu_t)
return self.menu_t
def topping_order(self): # Gets toppings the user wants and returns a total of all toppings.
topping_mappings = {
1: 'Bacon',
2: 'Pepperoni',
3: 'Mushrooms',
4: 'Anchovies',
5: 'Spinach',
6: 'Onions',
7: 'Black Olives',
8: 'Chicken',
9: 'Ground Beef'
}
self.requested_toppings = []
while True:
response = input('-')
if response == 'q':
break
toppings_wanted = response
toppings_wanted = topping_mappings[int(toppings_wanted)]
self.requested_toppings.append(toppings_wanted)
if toppings_wanted in topping_mappings.values():
print(f"Adding: {toppings_wanted}")
else:
print(f"We do not have {toppings_wanted}")
self.topping_total = len(self.requested_toppings) * float(1.23)
print("\nWe are adding the requested toppings to your pizza.")
print(f"your topping total will be: ${self.topping_total}")
return self.topping_total
def final_total(self):
total = self.size_cost + self.topping_total
total = float(total)
print(f"\nYour final order total will be ${total}")
if __name__ == '__main__':
customer_order = Pizza()
customer_order.size_menu()
customer_order.size_order()
customer_order.topping_menu()
customer_order.topping_order()
customer_order.final_total()
我想知道如果程序返回我正在寻找的信息,我为什么要使用__init__ 构造函数?
感谢您的帮助。
【问题讨论】:
-
把这些函数写成一个类有什么好处?您可以在一个模块中编写这些函数,导入该模块并实现类似的效果(如果不是相同的话)——有人会说这将是在 Python 中执行此操作的正确方法。
-
@wwii 感谢您的资源。我已经阅读了 Python 文档,但是仍然很难理解。我知道我可以将它们写入一个模块,但是我认为这段代码是解决问题和专注于 OOP 的绝佳机会。