6、建立一个汽车Auto类,包括:← 属性:汽车颜色color,车身重量weight、速度speed。+ 构造函数:能初始化各个属性值(speed初始值设为50)。4 方法:t speedup0:将属性值speed+10并显示speed值: speedCut 0:降属性值speed-10并显示speed值:4t show 0:显示属性值color、weight、speed.f 在主程序中创建实例并调初始化个属性值,调用show方法、加速、减速方
时间: 2024-03-19 15:45:16 浏览: 39
好的,我理解了你的问题。下面是汽车Auto类的代码实现:
```python
class Auto:
def __init__(self, color, weight, speed=50):
self.color = color
self.weight = weight
self.speed = speed
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)
```
在主程序中,你可以按照如下方式创建实例并调用各个方法:
```python
auto = Auto("红色", 1000)
auto.show() # 显示汽车属性值
auto.speedup() # 加速
auto.speedCut() # 减速
```
输出结果为:
```
汽车颜色: 红色
车身重量: 1000
当前速度为: 50
当前速度为: 60
当前速度为: 50
```
希望这个例子能够帮助你理解如何创建类、初始化属性、添加方法,并在主程序中调用它们。