|
6鱼币
这里面为什么是self.x.extend([beef, onion])呢?这句话没看懂 它的作用是啥呢?
- # 作者:小甲鱼
- # 来源:https://fishc.com.cn/thread-213592-1-1.html
- # 本代码著作权归作者所有,禁止商业或非商业转载,仅供个人学习使用,违者必究!
- # Copyright (c) fishc.com.cn All rights reserved
- class Meet:
- nums = 0
- class Egg(Meet):
- name = "鸡蛋"
- price = 1
- class Beef(Meet):
- name = "牛肉"
- price = 25
- class Mutoon(Meet):
- name = "羊肉"
- price = 30
- class Vegetable:
- nums = 0
- class Onion(Vegetable):
- name = "洋葱"
- price = 2
- class Tomato(Vegetable):
- name = "番茄"
- price = 2
- class Potato(Vegetable):
- name = "土豆"
- price = 3
- class Radish(Vegetable):
- name = "萝卜"
- price = 3
- class Menu:
- def order(self):
- self.x = []
- print("客官想要吃点什么?")
- print(type(self.x))
- dishes = input("1.洋葱炒牛肉;2.洋葱炒羊肉;3.煎蛋;4.番茄炒蛋;5.土豆萝卜炖羊肉:")
- dishes = dishes.split()
- while dishes:
- dish = dishes.pop(0)
-
- if dish == '1':
- onion = Onion()
- onion.num = 1
- beef = Beef()
- beef.num = 1
- self.x.extend([beef, onion])
- for each in self.x:
- print(each)
- if dish == '2':
- onion = Onion()
- onion.num = 1
- mutoon = Mutoon()
- mutoon.num = 1
- self.x.extend([mutoon, onion])
- if dish == '3':
- egg = Egg()
- egg.num = 2
- self.x.append(egg)
- if dish == '4':
- tomato = Tomato()
- tomato.num = 2
- egg = Egg()
- egg.num = 3
- self.x.extend([tomato, egg])
- if dish == '5':
- potato = Potato()
- potato.num = 2
- radish = Radish()
- radish.num = 1
- mutoon = Mutoon()
- mutoon.num = 2
- self.x.extend([potato, radish, mutoon])
-
- def pay(self):
- total = 0
- for each in self.x:
- print(each.name, each.price, "*", each.num)
- total += each.price * each.num
- print(f"感谢惠顾,您一共消费了 {total} 元,欢迎下次光临~")
复制代码
这个代码中定义了多个类,它们分别代表不同的食材和菜品。其中 Menu 类负责处理顾客的点餐过程和结账。
在 Menu 类中,order 方法用于处理用户输入的点菜信息。self.x 是一个列表,用于存储顾客点的菜品中所包含的食材。self.x.extend([beef, onion]) 这句话的作用是将两个食材(牛肉和洋葱)添加到 self.x 列表中。
这里的 extend 方法是 Python 中列表对象的一个方法,它接受一个可迭代对象作为参数(这里是一个列表),并将其元素添加到原列表的末尾。在这个例子中,beef 和 onion 两个对象分别表示牛肉和洋葱这两种食材,它们都是类实例。
简而言之,self.x.extend([beef, onion]) 这句话的作用就是将牛肉和洋葱这两种食材添加到顾客点的菜品的食材列表中。
|
最佳答案
查看完整内容
这个代码中定义了多个类,它们分别代表不同的食材和菜品。其中 Menu 类负责处理顾客的点餐过程和结账。
在 Menu 类中,order 方法用于处理用户输入的点菜信息。self.x 是一个列表,用于存储顾客点的菜品中所包含的食材。self.x.extend() 这句话的作用是将两个食材(牛肉和洋葱)添加到 self.x 列表中。
这里的 extend 方法是 Python 中列表对象的一个方法,它接受一个可迭代对象作为参数(这里是一个列表),并将其元素添加 ...
|