交通工具有多种多样,比如:汽车、火车、飞机等,一般的交通工具类的属性有:厂商,发动机型号,制作型号,时间;方法有:运行(输出速度),停止。 (1)请使用抽象方法实现交通工具类和方法:实现汽车、火车、飞机类,继承于交通工具类,实现他们不同的运行方法。 (2)Tom在出门去,不知道会乘坐什么交通工具,利用用户的输入值进行随机派遣(mod 3 ,根据余数随机决定交通工具),要求程序利用上转型对象完成编程。

时间: 2023-06-02 08:06:10 浏览: 78
(1)交通工具类和方法的抽象实现: ```python from abc import ABC, abstractmethod class Transportation(ABC): @abstractmethod def run(self): pass @abstractmethod def stop(self): pass class Car(Transportation): def __init__(self, brand, engine_model, production_model, year): self.brand = brand self.engine_model = engine_model self.production_model = production_model self.year = year def run(self): print("Car is running.") def stop(self): print("Car is stopped.") class Train(Transportation): def __init__(self, brand, engine_model, production_model, year): self.brand = brand self.engine_model = engine_model self.production_model = production_model self.year = year def run(self): print("Train is running.") def stop(self): print("Train is stopped.") class Airplane(Transportation): def __init__(self, brand, engine_model, production_model, year): self.brand = brand self.engine_model = engine_model self.production_model = production_model self.year = year def run(self): print("Airplane is flying.") def stop(self): print("Airplane is landed.") ``` (2)根据用户输入的随机值,随机派遣交通工具: ```python import random def dispatch_vehicle(num): if num == 0: return Car("Volvo", "XL-1000", "SUV-2", "2019") elif num == 1: return Train("CRH-500", "AC-1000", "Passenger-2", "2021") elif num == 2: return Airplane("Boeing", "PW-2000", "747-400", "2010") else: return None # 随机生成模数 input_num = random.randint(1, 100) vehicle = dispatch_vehicle(input_num % 3) if vehicle is not None: vehicle.run() else: print("No vehicle dispatched.") ```

相关推荐

最新推荐

recommend-type

51单片机DIY制作实例:旋转LED数字显示电子钟(含C语言源程序)

该文是一篇基于51单片机的DIY制作详述,作者此时是51单片机初学者,这个制作也可以作为不少单片机学习者的练手实验,如进行多样衍生会得到不错的设计。
recommend-type

城市形态、交通能耗和环境影响集成的多智能体模型

城市能耗占全球能耗的比重随着城市化率的不断提高而增大,交通能耗作为城市能耗的重要构成部分,已有较多研究证明城市形态对其具有显著影响,这些研究多属于城市间层次,而少有城市内的研究对城市形态与交通能耗、环境...
recommend-type

RFID技术中的RFID系统按照工作频率进行分类

其基本特点是标签的成本较低、标签内保存的数据量较少、阅读距离较短(无源情况9典型阅读距离为10cm)、电子标签外形多样(卡状、环状、钮扣状、笔状)、阅读天线方向性不强等。  (2)中高频系统  中高频系统的...
recommend-type

脉冲信号和电平信号到底有什么区别

脉冲信号是一种离散信号,形状多种多样,与普通模拟信号(如正弦波)相比,波形之间在时间轴不连续(波形与波形之间有明显的间隔)但具有一定的周期性是它的特点。最常见的脉冲波是矩形波(也就是方波)。脉冲信号...
recommend-type

《数字电路与逻辑设计》课程设计论文交通信号灯设计

随着各种交通工具的发展和交通指挥的需要,第一盏名副其实的三色灯(红、黄、绿三种标志)于1918年诞生。它是三色圆形四面投影器,被安装在纽约市五号街的一座高塔上,由于它的诞生,使城市交通大为改善。 当前,大量...
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。