(1)创建Car类,添加车轮数和颜色两个属性。 (2)定义第1个函数,并增加参数name,输出“name有4个车轮,颜色是红色。 (3)定义第2个函数,输出“车行驶在学习的大道上。”。 (4)调用Car类,赋值给新变量。 (5)用新变量调用定义好的两个函数。
时间: 2024-05-09 12:21:50 浏览: 113
好的,我知道了!以下是代码实现:
(1):
```python
class Car:
def __init__(self, wheels, color):
self.wheels = wheels
self.color = color
```
(2):
```python
class Car:
def __init__(self, wheels, color):
self.wheels = wheels
self.color = color
def print_info(self, name):
print(name + "有" + str(self.wheels) + "个车轮,颜色是" + self.color + "。")
```
(3):
```python
class Car:
def __init__(self, wheels, color):
self.wheels = wheels
self.color = color
def print_info(self, name):
print(name + "有" + str(self.wheels) + "个车轮,颜色是" + self.color + "。")
def drive(self):
print("车行驶在学习的大道上。")
```
(4):
```python
my_car = Car(4, "红色")
```
(5):
```python
my_car.print_info("我的车")
my_car.drive()
```
注意,如果您要在 Python 命令行环境下运行此代码,请按照相应的顺序一次性将所有代码输入,否则可能会抛出语法错误或错误结果。