创建交通工具类,包含速度和前往目的地的方法,飞机类,轮船类,汽车类,步行类,跑步类继承交通工具类,重写前往方法,输入为“交通工具\n速度\n“,输出为”We can 方法to our destination, the speed is 速度km/h\n“,交通工具不存在则输出“Error\n”
时间: 2023-05-17 20:07:35 浏览: 74
以下是交通工具类的代码实现,包含速度和前往目的地的方法,以及飞机类、轮船类、汽车类、步行类、跑步类的继承和重写:
```python
class Transportation:
def __init__(self, speed):
self.speed = speed
def go_to(self, destination):
print(f"We can go to our destination, the speed is {self.speed} km/h")
class Airplane(Transportation):
def go_to(self, destination):
print(f"We can fly to our destination, the speed is {self.speed} km/h")
class Ship(Transportation):
def go_to(self, destination):
print(f"We can sail to our destination, the speed is {self.speed} km/h")
class Car(Transportation):
def go_to(self, destination):
print(f"We can drive to our destination, the speed is {self.speed} km/h")
class Walk(Transportation):
def go_to(self, destination):
print(f"We can walk to our destination, the speed is {self.speed} km/h")
class Run(Transportation):
def go_to(self, destination):
print(f"We can run to our destination, the speed is {self.speed} km/h")
def create_transportation(transportation_type, speed):
if transportation_type == "Airplane":
return Airplane(speed)
elif transportation_type == "Ship":
return Ship(speed)
elif transportation_type == "Car":
return Car(speed)
elif transportation_type == "Walk":
return Walk(speed)
elif transportation_type == "Run":
return Run(speed)
else:
print("Error")
# 示例用法
t = create_transportation("Airplane", 800)
t.go_to("New York")
```
输入为“交通工具\n速度\n”,可以通过以下代码实现:
```python
transportation_type, speed = input().split()
t = create_transportation(transportation_type, int(speed))
t.go_to("destination")
```
如果输入的交通工具不存在,则输出“Error”。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)