现有一项业务:“Joker在BMW 4S店买了一辆BMW X7”,根据 业务描述,声明相关类。并实例化。
时间: 2024-12-21 09:29:33 浏览: 7
为了表示这项业务,我们可以创建三个相关的类:Person、Car和Store。这里是一个简单的Python示例:
```python
class Person:
def __init__(self, name):
self.name = name
class Car:
def __init__(self, brand, model):
self.brand = brand
self.model = model
class Store:
def __init__(self, name, car_model):
self.name = name
self.car_model = car_model
# 实例化人物
joker = Person("Joker")
# 实例化车辆
bmw_x7 = Car("BMW", "X7")
# 实例化4S店
bmw_4s_store = Store("BMW 4S店", "X7")
# 表明Joker在店里买了车
joker.purchased_car = bmw_4s_store
bmw_4s_store.sales = [bmw_x7]
# 输出购买信息
print(f"{joker.name}在{bmw_4s_store.name}购买了{bmw_x7.brand} {bmw_x7.model}.")
阅读全文