class Product: def __init__(self, name, weight, price): self.name = name self.weight = weight self.price = price def get_price(self): return self.price class User: def __init__(self, name): self.name = name def inquire_price(self, product): return product.get_price() product1 = Product("甜点", 0.5, 5.0) product2 = Product("饮品", 0.3, 3.0) user1 = User("jack") user2 = User("dio") print(user1.name, "询问", product1.name, "的价格:", user1.inquire_price(product1)) print(user2.name, "询问", product2.name, "的价格:", user2.inquire_price(product2))
时间: 2024-02-23 18:56:05 浏览: 123
这段代码是一个简单的面向对象的程序,其中定义了两个类:Product(产品)和User(用户)。Product类有三个属性:name(名称)、weight(重量)和price(价格),以及一个方法get_price(获取价格),用于获取产品的价格。User类有一个属性name(名称)和一个方法inquire_price(查询价格),用于查询某个产品的价格。
在主程序中,定义了两个产品product1和product2,以及两个用户user1和user2。然后分别调用了每个用户的inquire_price方法,查询了product1和product2的价格,并输出了结果。
相关问题
网购订单模拟 ▪ 顾客类(属性/字段:姓名、地址、订单列表(数组);方法:1.计算所有订单的总价、 总重量、和总运费,2.打印总价和运费) ▪ 订单类(属性/字段:购买商品列表(数组);方法:计算订单总价,计算总
重量,计算总运费) ▪ 商品类(属性/字段:名称、价格、重量)
以下是代码示例:
```python
class Product:
def __init__(self, name, price, weight):
self.name = name
self.price = price
self.weight = weight
class Order:
def __init__(self, products):
self.products = products
def calculate_total_price(self):
total_price = 0
for product in self.products:
total_price += product.price
return total_price
def calculate_total_weight(self):
total_weight = 0
for product in self.products:
total_weight += product.weight
return total_weight
def calculate_total_shipping_cost(self):
total_weight = self.calculate_total_weight()
total_shipping_cost = 0
if total_weight <= 10:
total_shipping_cost = 5
elif total_weight > 10 and total_weight <= 20:
total_shipping_cost = 10
elif total_weight > 20 and total_weight <= 30:
total_shipping_cost = 15
else:
total_shipping_cost = 20
return total_shipping_cost
class Customer:
def __init__(self, name, address, orders):
self.name = name
self.address = address
self.orders = orders
def calculate_total_price(self):
total_price = 0
for order in self.orders:
total_price += order.calculate_total_price()
return total_price
def calculate_total_weight(self):
total_weight = 0
for order in self.orders:
total_weight += order.calculate_total_weight()
return total_weight
def calculate_total_shipping_cost(self):
total_shipping_cost = 0
for order in self.orders:
total_shipping_cost += order.calculate_total_shipping_cost()
return total_shipping_cost
def print_total_price_and_shipping_cost(self):
total_price = self.calculate_total_price()
total_shipping_cost = self.calculate_total_shipping_cost()
print("Total price: ${}".format(total_price))
print("Total shipping cost: ${}".format(total_shipping_cost))
```
使用示例:
```python
product1 = Product("iPhone", 999, 0.5)
product2 = Product("MacBook", 1499, 1.5)
product3 = Product("iPad", 599, 0.8)
order1 = Order([product1, product2])
order2 = Order([product3])
customer = Customer("John", "123 Main St, Anytown, USA", [order1, order2])
customer.print_total_price_and_shipping_cost()
```
输出结果:
```
Total price: $2498
Total shipping cost: $15
```
分别实现产品类和用户类,产品对象包含产品名称、产品重量和产品价格等;用户对象包含用户名称;用户对象可以向产品对象询问价格,产品对象接到询价时会回答该产品的价格。,user中有inquiries()方法,product有quote(()方法,给出代码实现
以下是Python的示例代码实现:
```python
class Product:
def __init__(self, name, weight, price):
self.name = name
self.weight = weight
self.price = price
def quote(self):
return self.price
class User:
def __init__(self, name):
self.name = name
def inquiries(self, product):
return product.quote()
# 测试代码
p1 = Product('Apple', 0.2, 5.0)
p2 = Product('Banana', 0.3, 3.0)
u1 = User('John')
u2 = User('Lisa')
print(u1.inquiries(p1)) # 输出 5.0
print(u2.inquiries(p2)) # 输出 3.0
```
以上代码定义了 Product 类和 User 类,其中 Product 类包含了产品的名称、重量和价格等属性,以及一个 quote() 方法用于返回产品的价格。User 类包含了用户的名称和一个 inquiries() 方法,该方法接收一个 Product 对象作为参数,调用该对象的 quote() 方法获取价格并返回。
在测试代码中,我们创建了两个 Product 对象 p1 和 p2,以及两个 User 对象 u1 和 u2。然后分别调用了 u1 和 u2 的 inquiries() 方法,传入不同的 Product 对象作为参数,输出了查询到的价格。
阅读全文