当前位置:网站首页>Understanding and function of polymorphism
Understanding and function of polymorphism
2022-07-02 19:33:00 【Charmaine Xia】
One 、 What is polymorphism
The definition of polymorphism in object-oriented : It refers to providing a unified interface for entities with different data types .
For example, there are animals (Animal) The category of (Class), And animals inherit the class chicken (Chicken) And category dog (Dog), And for animals of the same origin ( Parent class ) One message has a different response , For example, the category of animals has “ It's called ()” The action of , And the type of chicken will “ Cry ()”, Category dogs will “ bark ()”, It is called polymorphism .
Two 、 Polymorphic type
Polymorphism can be divided into static polymorphism and dynamic polymorphism .
- Static polymorphism : Implement polymorphism during compilation , The same method passes different parameters , Achieve different effects .
C++ Code implementation :
// print() function
void print(const char* str,int width);
void print(double i ,int width);
void print(const char* str);
// Use print() Function time , The compiler will use prototypes with corresponding characteristics according to the usage taken
print("abc",12);
print(2.2,55);
print("def");
- Dynamic polymorphism : Realize polymorphism during operation , A class reference can be assigned to different subclass objects .
The necessary condition to realize dynamic polymorphism :
- Programming languages should support inheritance
- Subclasses can override (override) Methods in the parent class
- Parent references can point to child objects
C# Code implementation :
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;
}
}
}
3、 ... and 、 What problem does polymorphism solve
- The application does not have to write function calls for each subclass , Just process the parent class . Greatly improve the reusability of the program .
- The function of the subclass can be called by the method or reference variable of the parent class , It's like Animal class , Can improve scalability and maintainability .
边栏推荐
- 《代码整洁之道》读书笔记
- Implementation of 452 strcpy, strcat, StrCmp, strstr, strchr
- MySQL
- Qpropertyanimation use and toast case list in QT
- Virtual machine initialization script, virtual machine mutual secret key free
- AcWing 1129. 热浪 题解(最短路—spfa)
- Markdown basic grammar
- AcWing 1134. 最短路计数 题解(最短路)
- In pytorch function__ call__ And forward functions
- mybatiesHelperPro工具必须的可以生成到对应项目文件夹下
猜你喜欢
Machine learning notes - time series prediction research: monthly sales of French champagne
Usage of ieda refactor
Registration opportunity of autowiredannotationbeanpostprocessor in XML development mode
教程篇(5.0) 09. RESTful API * FortiEDR * Fortinet 网络安全专家 NSE 5
450-深信服面经1
《重构:改善既有代码的设计》读书笔记(上)
[error record] problems related to the installation of the shuttle environment (follow-up error handling after executing the shuttle doctor command)
搭建哨兵模式reids、redis从节点脱离哨兵集群
High frequency interview questions
Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径
随机推荐
AcWing 1134. 最短路计数 题解(最短路)
MySQL
Use cheat engine to modify money, life and stars in Kingdom rush
Npoi export Excel2007
使用 Cheat Engine 修改 Kingdom Rush 中的金钱、生命、星
PHP非对称加密方法私钥及公钥加密解密的方法
Idea editor removes SQL statement background color SQL statement warning no data sources are configured to run this SQL And SQL dialect is not config
AcWing 1131. 拯救大兵瑞恩 题解(最短路)
Golang并发编程——goroutine、channel、sync
Emmet basic syntax
全志A33使用主线U-Boot
PHP-Parser羽毛球预约小程序开发require线上系统
搭建主从模式集群redis
The mybatieshelperpro tool can be generated to the corresponding project folder if necessary
AcWing 341. 最优贸易 题解 (最短路、dp)
横向越权与纵向越权[通俗易懂]
高级性能测试系列《24. 通过jdbc执行sql脚本》
程序猿入门攻略(十二)——数据的存储
电脑使用哪个录制视频软件比较好
Golang:[]byte to string