The different types of polymorphism in OOP-面向对象程序设计中的多态类型

本文介绍了计算机语言中四种类型的多态性:强制、重载、参数化和包含。通过实例详细解释了每种多态性的特点及应用场景,如类型转换、方法重载和泛型类的设计。

There are four kinds of polymorphism at the computer-language level:      

coercion, overloading, parametric, and inclusion. 

Coercion polymorphism refers to a single operation serving several types through implicit type conversion. For example, the subtraction operation manifests itself in source code through the subtraction operator (-). That operator allows you to subtract an integer from another integer and a floating-point value from another floating-point value. However, if one operand is an integer and the other operand a floating-point value, the compiler must convert the integer's operand type to floating-point. Otherwise, a type error occurs -- because Java's subtraction operation does not subtract integers from floating-point values, or vice versa. Another example of coercion polymorphism involves method calls. If a subclass declares a method with a superclass parameter, and if a call is made to that method with a subclass object reference, the compiler implicitly converts the subclass reference type to the superclass reference type. That way, only superclass-defined operations are legal (without explicit type casts) in the method.

Overloading polymorphism refers to using a single identifier for different operations. For example, the + identifier -- I use the term identifier in a general sense -- signifies any one of several operations based on its operands' types. If both operands have integer types, the integer addition operation occurs. Similarly, if both operands have floating-point types, a floating-point operation occurs. Finally, if those operands are strings, string concatenation is guaranteed to be the operation.Along with its language-defined operator overloading, Java also permits method names to overload -- as long as the number and/or types of each method's parameters differ. That way, the same method-name identifier can apply to different operations. 

Parametric polymorphism refers to a class declaration that allows the same field names and method signatures to associate with a different type in each instance of that class. For example, you might create a LinkedList class with a data field that holds any type of data. To allow for proper type checking at compile time, you do not want to give that field Object type (as in Object data;) as the compiler cannot inform you if the code attempts to perform invalid operations on data, because only the JVM knows the actual type of data at runtime. However, you do not want to tie data to a specific type in your source code, because you lose the benefit of being able to store different object types in your LinkedList objects. To achieve the best of both worlds, parametric polymorphism gives you the benefits of static type checking -- which alerts you to attempts that perform invalid operations on data -- and allows data to hold different object types. Probably the best-known example of parametric polymorphism is the C++ template language feature. Although Java does not officially support parametric polymorphism, developers expect Sun to add that support to version 1.5 of the SDK, which is scheduled for a 2003 release.

Inclusion polymorphism refers to a situation in which a type can be another type's subtype. Every subtype value can appear in a supertype context, where the execution of the supertype's operations (on that value) results in the execution of the subtype's equivalent operations. For that reason, inclusion polymorphism is also known as subtype polymorphism. To understand inclusion (or subtype) polymorphism, you must understand how Java binds method calls to classes and objects.

打开链接下载源码: https://pan.quark.cn/s/a4b39357ea24 在Qt框架中,QSerialPort类被视为一个关键组件,用于执行与串行端口之间的通信任务,它具备多样化的功能,涵盖了串口的开启与关闭操作,以及波特率、数据位、停止位和奇偶校验等参数的设定,同时还包括数据的发送和接收功能。在标题和描述中提及的“Qt5的QSerialPort类通过信号槽实现串口读写”,这代表了一种在Qt编程中普遍采用的事件驱动策略,借助信号槽机制,能够便捷地管理串口数据的传输与接收。 1. **QSerialPort类的基础操作**: - 初始化阶段:必须构建一个QSerialPort实例,并为其指定串口名称,例如"/dev/ttyUSB0"。 - 参数配置:利用`setPortName()`、`setBaudRate()`、`setDataBits()`、`setParity()`、`setStopBits()`、`setFlowControl()`等方法,依据具体需求对串口参数进行配置。 - 串口开启/终止:借助`open()`方法启动串口,通过`close()`方法终止串口。务必验证`isOpen()`的返回状态,以确保操作的有效性。 2. **信号槽机制的应用**: - 信号的生成:QSerialPort类中定义了若干信号,诸如`readyRead()`表明有数据可读,`error()`指示出现错误,`bytesWritten()`显示数据已传输等。当这些事件发生时,将触发相应的信号。 - 槽函数的关联:相应地,可以将这些信号与自定义的槽函数相连接,比如,当`readyRead()`信号被激活时,可以调用一个用于处理读取数据的函数。 3. **串口数据...
内容概要:本文档聚焦于超宽带(UWB)技术的核心研究,系统探讨了干扰对齐与抵消机制、UWB单天线与多天线系统的建模与仿真,并提供了完整的Matlab代码实现方案。文档强调科研工作不仅需要严谨的逻辑与扎实的努力,更应注重“借力”思维与创新突破,建议读者按照知识体系循序渐进地学习,避免陷入碎片化理解的困境。除UWB专题外,文档还全面展示了基于Matlab/Simulink的多领域科研支持能力,涵盖智能优化算法、机器学习、电力系统、路径规划、通信与信号处理、图像融合、雷达追踪、车间调度等多个前沿方向,形成了一套完整的科研方法论与技术生态体系。所有相关资源可通过指定公众号或百度网盘获取,便于快速复现与二次开发。; 适合人群:具备一定Matlab编程基础和通信系统理论知识,从事电子信息、通信工程、自动化、电力系统及相关交叉学科的研究生、科研人员及工程技术人员。; 使用场景及目标:①掌握UWB系统中干扰抑制与天线设计的关键技术原理;②利用配套Matlab代码完成算法仿真、性能验证与参数优化;③借鉴成熟的优化模型与仿真框架,拓展至自身研究课题如路径规划、微电网调度、信号处理等;④通过复现高水平论文模型,提升科研实践能力与学术竞争力。; 阅读建议:建议严格按照文档的知识结构顺序阅读,优先聚焦与自身研究方向契合的内容模块,结合提供的Matlab代码动手实践,积极利用公众号“荔枝科研社”及百度网盘中的完整资源包,实现从理论理解到项目落地的高效转化。
已经博主授权,源码转载自 https://pan.quark.cn/s/a4b39357ea24 ### 批处理脚本实现指定文件夹内所有文件与子目录的移除 #### 简介 在Windows系统环境下,批处理脚本是一种极具价值的应用工具,它能够协助用户执行一系列预先设定好的指令,达成自动化处理的目的。本说明着重阐述如何借助批处理脚本移除特定文件夹内的全部文件及子文件夹,并对几种常用技巧的效果进行剖析。 #### 批处理脚本的基础知识 批处理脚本是一种基于DOS命令行环境构建的文本性文档,其文件后缀为`.bat`。借助编写批处理脚本,使用者可以完成复杂任务流程的自动化,例如文件复制、移动、清除等动作。 #### 第一种方法:运用`RD`指令 `RD`指令专用于移除目录(即文件夹)。该指令的标准格式如下所示: ```batch RD [drive:]path [parameters] ``` 其中,`[drive:]path`代表待清除的目录路径,`[parameters]`为若干可选参数,常用的包括: - `/S`:递归式地移除目录及其所有嵌套子目录。 - `/Q`:执行静默模式,不进行确认提示。 ##### 示例1:直接运用`RD`指令 若采用`RD /S /Q c:\temp`指令来移除`C:\temp`目录中的所有文件及子文件夹,将连同`temp`目录本体一同被清除。 ```batch rd /s /q c:\temp ``` #### 第二种方法:灵活运用`RD`指令 为防止误删`temp`目录本身,可以通过先利用`RD`指令清空`temp`目录内的所有内容,随后重新构建`temp`目录的技巧来实现。 ##### 示例2:灵活运用`RD`指令 ```batch rd ...
已经博主授权,源码转载自 https://pan.quark.cn/s/a4b39357ea24 在“WEB前端-案例汇总”这一资源集合中,收录了大量的前端开发实践范例,其核心目的在于引导初学者逐步提升,并系统性地掌握前端开发所需的关键技能。这个广泛的案例合集几乎包罗了前端开发的所有重要范畴,对于渴望深入研究和理解Web前端技术的人来说,无疑是一份极具价值的参考资料。 1. HTML基础:HTML(超文本标记语言)是网页构建的根基,其涉及的基本构成要素包括标记、属性以及结构等。相关的实例可能涵盖基础的静态页面构建,例如个人履历、产品介绍页面等,通过这些范例,学习者可以领会到如何合理地安排网页的内容与结构。 2. CSS样式设计:CSS(层叠样式表)主要用于调控网页的布局与视觉呈现。相关的案例或许会涉及盒模型、选择器、浮动、定位以及响应式设计等,使学习者能够设计出既美观又能适应不同设备的页面。 3. JavaScript交互:JavaScript作为前端开发的核心,负责实现动态效果与用户交互功能。相关的实例可能包含事件管理、文档对象模型操作、异步JavaScript与XML请求、函数及对象的应用等,通过这些实例,学习者能够学会如何增强网页的互动性。 4. jQuery库的应用:jQuery简化了JavaScript的操作,提供了功能丰富的接口和插件。相关的案例或许会涉及动画效果、文档对象模型操作、事件管理等方面,使初学者能够迅速掌握并提高开发效率。 5. 响应式设计:随着移动设备的广泛使用,响应式设计已成为一项必备技能。相关的案例可能包括运用媒体查询、弹性盒模型或网格布局来达成不同屏幕尺寸下的适配效果。 6. 模块化与框架:在现代前端开发实践中,Vu...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值