python写程序:6、建立一个汽车Auto类,包括: 属性:汽车颜色color,车身重量weight、速度speed。 构造函数:能初始化各个属性值(speed初始值设为50)。 方法: speedup():将属性值speed+10并显示speed值; speedCut():降属性值speed-10并显示speed值; show ():显示属性值color、weight、speed。 在主程序中创建实例并调初始化个属性值,调用show方法、加速、减速方法。
时间: 2024-03-24 12:36:17 浏览: 22
好的,以下是实现该程序的代码:
```python
class Auto:
def __init__(self, color, weight):
self.color = color
self.weight = weight
self.speed = 50
def speedup(self):
self.speed += 10
print("加速后的速度为:", self.speed)
def speedCut(self):
self.speed -= 10
print("减速后的速度为:", self.speed)
def show(self):
print("汽车颜色:", self.color)
print("车身重量:", self.weight)
print("汽车速度:", self.speed)
car = Auto("red", 1000)
car.show()
car.speedup()
car.speedCut()
```
运行结果:
```
汽车颜色: red
车身重量: 1000
汽车速度: 50
加速后的速度为: 60
减速后的速度为: 50
```