2.定义一个汽车类,并在类中定义一个move方法,然后分别创建BMW_X9、AUDI_A9对象,并添加颜色、马力、型号等属性,然后分别打印出属性值、调用move方法(使用init方法完成属性赋值)
时间: 2024-02-01 18:15:26 浏览: 134
AODV.rar_VANET ns2_ns2 vanet_sumo VANET_sumo move_vanet
5星 · 资源好评率100%
下面是代码实现:
```python
class Car:
def __init__(self, color, horsepower, model):
self.color = color
self.horsepower = horsepower
self.model = model
def move(self):
print(f"{self.model} is moving.")
BMW_X9 = Car("black", "350 hp", "BMW X9")
AUDI_A9 = Car("white", "400 hp", "AUDI A9")
print(f"BMW_X9: color={BMW_X9.color}, horsepower={BMW_X9.horsepower}, model={BMW_X9.model}")
BMW_X9.move()
print(f"AUDI_A9: color={AUDI_A9.color}, horsepower={AUDI_A9.horsepower}, model={AUDI_A9.model}")
AUDI_A9.move()
```
输出结果为:
```
BMW_X9: color=black, horsepower=350 hp, model=BMW X9
BMW X9 is moving.
AUDI_A9: color=white, horsepower=400 hp, model=AUDI A9
AUDI A9 is moving.
```
阅读全文