Python 搬家具练习题
时间: 2023-12-25 13:29:29 浏览: 101
以下是一个Python搬家具练习题的例子:
```python
# 定义一个家具类
class Furniture:
def __init__(self, name, weight):
self.name = name
self.weight = weight
# 定义一个搬运工类
class Mover:
def __init__(self, name):
self.name = name
self.furniture_list = []
def add_furniture(self, furniture):
self.furniture_list.append(furniture)
def get_total_weight(self):
total_weight = 0
for furniture in self.furniture_list:
total_weight += furniture.weight
return total_weight
# 创建家具对象
chair = Furniture("Chair", 10)
table = Furniture("Table", 20)
bed = Furniture("Bed", 50)
# 创建搬运工对象
mover = Mover("John")
# 添加家具到搬运工对象
mover.add_furniture(chair)
mover.add_furniture(table)
mover.add_furniture(bed)
# 获取搬运工搬运的家具总重量
total_weight = mover.get_total_weight()
print("The total weight of the furniture is:", total_weight) # 输出:The total weight of the furniture is: 80
```
阅读全文