用Python编写一个程序,定义一个汽车类,并在类中定义一个move方法,然后分别创建BUDI_A9对象,并添加颜MW_X9、A色、马力、型号等属性,然后分别打印出属性值、调用move方法(使用__init__方法完成属性赋值)
时间: 2023-06-24 09:06:22 浏览: 175
可以参考以下代码实现:
```python
class Car:
def __init__(self, brand, color, horsepower, model):
self.brand = brand
self.color = color
self.horsepower = horsepower
self.model = model
def move(self):
print(f"{self.brand} {self.model} is moving.")
budi_a9 = Car("BUDI_A9", "black", 200, "A9X")
bmw_x9 = Car("BMW_X9", "red", 250, "X9S")
print(f"BUDI_A9's color is {budi_a9.color}, horsepower is {budi_a9.horsepower}.")
budi_a9.move()
print(f"BMW_X9's color is {bmw_x9.color}, horsepower is {bmw_x9.horsepower}.")
bmw_x9.move()
```
输出结果:
```
BUDI_A9's color is black, horsepower is 200.
BUDI_A9 A9X is moving.
BMW_X9's color is red, horsepower is 250.
BMW_X9 X9S is moving.
```
阅读全文