请以一次“车展”为主题,编写程序介绍车展的基本情况,要求: (1)请自行创建多个"汽车"对象,并初始化每个"汽车"(包括价格,颜色,车轮数量,车型等属性)。 (2)请自行设计不同的行驶场景,在不同的场景中显示“汽车”的行驶状态。 (3)采用多态的方法实现不同“汽车”对象的基本介绍。
时间: 2024-02-25 22:59:13 浏览: 7
好的,我可以为您编写这样一个关于车展的程序。
首先,我们创建几个不同的汽车对象,并初始化它们的属性:
```python
class Car:
def __init__(self, price, color, wheel_num, car_type):
self.price = price
self.color = color
self.wheel_num = wheel_num
self.car_type = car_type
class SportsCar(Car):
def __init__(self, price, color):
super().__init__(price, color, 4, "Sports Car")
class SUV(Car):
def __init__(self, price, color):
super().__init__(price, color, 4, "SUV")
class Truck(Car):
def __init__(self, price, color):
super().__init__(price, color, 6, "Truck")
sports_car = SportsCar(1000000, "Red")
suv = SUV(500000, "Blue")
truck = Truck(800000, "Black")
```
接下来,我们设计不同的行驶场景,来展示汽车的行驶状态。比如:
```python
def drive_on_highway(car):
print(f"The {car.car_type} is driving on the highway at 100 km/h")
def drive_on_mountain_road(car):
print(f"The {car.car_type} is driving on a mountain road at 50 km/h")
def drive_in_city(car):
print(f"The {car.car_type} is driving in the city at 30 km/h")
```
最后,我们采用多态的方法实现不同汽车对象的基本介绍:
```python
def introduce(car):
print(f"This is a {car.car_type} with {car.wheel_num} wheels, {car.color} color, and a price of {car.price} yuan.")
introduce(sports_car)
introduce(suv)
introduce(truck)
```
这样,我们就完成了一个简单的关于车展的程序,它可以展示不同汽车的行驶状态,并且采用多态的方法实现了不同汽车对象的基本介绍。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)