当前位置:网站首页>多态的理解以及作用
多态的理解以及作用
2022-07-02 18:23:00 【夏诗曼CharmaineXia】
一、多态是什么
多态在面向对象中的定义:指为不同数据类型的实体提供统一的接口。
比如有动物(Animal)之类别(Class),而且由动物继承出类别鸡(Chicken)和类别狗(Dog),并对同一源自类别动物(父类)之一消息有不同的响应,如类别动物有“叫()”之动作,而类别鸡会“啼叫()”,类别狗则会“吠叫()”,则称之为多态。
二、多态的类型
多态分为静态多态和动态多态。
- 静态多态:在编译期间实现多态,相同的方法传递不同的参数,实现不同的效果。
C++代码实现:
// print()函数
void print(const char* str,int width);
void print(double i ,int width);
void print(const char* str);
// 使用print()函数时,编译器将根据所采取的用法使用有相应特征标的原型
print("abc",12);
print(2.2,55);
print("def");
- 动态多态:在运行期间实现多态,一个类的引用可以被赋值为不同的子类对象。
实现动态多态的必要条件:
- 编程语言要支持继承
- 子类可以重写(override)父类中的方法
- 父类引用可以指向子类对象
C#代码实现:
public class AnimalFactory{
public static Animal createAnimal(string type){
Animal animla=null;
switch(type){
case "cat":
animal=new Cat();
break;
case "bird":
animal=new Bird();
break;
case "dog":
animal=new Dog();
break;
}
}
}
三、多态解决什么问题
- 应用程序不必为每一个子类编写功能调用,只需要对父类进行处理即可。大大提高程序的可复用性。
- 子类的功能可以被父类的方法或引用变量所调用,就像Animal类,可以提高可扩充性和可维护性。
边栏推荐
- 搭建哨兵模式reids、redis从节点脱离哨兵集群
- #gStore-weekly | gStore源码解析(四):安全机制之黑白名单配置解析
- MySQL高级(进阶)SQL语句
- 使用 Cheat Engine 修改 Kingdom Rush 中的金钱、生命、星
- Gstore weekly gstore source code analysis (4): black and white list configuration analysis of security mechanism
- Juypter notebook modify the default open folder and default browser
- Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3
- 开发固定资产管理系统,开发固定资产管理系统用什么语音
- MySQL表历史数据清理总结
- 搭建主从模式集群redis
猜你喜欢
冒泡排序数组
![[error record] problems related to the installation of the shuttle environment (follow-up error handling after executing the shuttle doctor command)](/img/c1/a00425f2e1824a50644c8fbb15fe38.jpg)
[error record] problems related to the installation of the shuttle environment (follow-up error handling after executing the shuttle doctor command)

注解开发方式下AutowiredAnnotationBeanPostProcessor的注册时机

Usage of ieda refactor

ICDE 2023|TKDE Poster Session(CFP)

Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径

Introduction to the paper | analysis and criticism of using the pre training language model as a knowledge base

高频面试题

Yunna | why use the fixed asset management system and how to enable it

450-深信服面经1
随机推荐
Juypter notebook modify the default open folder and default browser
预处理和预处理宏
[0701] [论文阅读] Alleviating Data Imbalance Issue with Perturbed Input During Inference
End to end object detection with transformers (Detr) paper reading and understanding
2022.7.1-----leetcode.241
Imitation Jingdong magnifying glass effect (pink teacher version)
xml开发方式下AutowiredAnnotationBeanPostProcessor的注册时机
[论文阅读] CA-Net: Leveraging Contextual Features for Lung Cancer Prediction
mysql备份后缀是什么_mysql备份还原
《架构整洁之道》读书笔记(下)
高频面试题
Horizontal ultra vires and vertical ultra vires [easy to understand]
Tutorial (5.0) 10 Troubleshooting * fortiedr * Fortinet network security expert NSE 5
ICDE 2023|TKDE Poster Session(CFP)
Introduction of Ethernet PHY layer chip lan8720a
论文导读 | 关于将预训练语言模型作为知识库的分析与批评
Mobile robot path planning: artificial potential field method [easy to understand]
电脑使用哪个录制视频软件比较好
Virtual machine initialization script, virtual machine mutual secret key free
High frequency interview questions