当前位置:网站首页>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 .
边栏推荐
- MySQL advanced (Advanced) SQL statement
- Fastdfs installation
- Reading notes of code neatness
- Memory management of C
- 《重构:改善既有代码的设计》读书笔记(上)
- AcWing 1126. 最小花费 题解(最短路—dijkstra)
- Use cheat engine to modify money, life and stars in Kingdom rush
- zabbix5客户端安装和配置
- NPOI导出Excel2007
- Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3
猜你喜欢
数据降维——因子分析
冒泡排序数组
450-深信服面经1
Develop fixed asset management system, what voice is used to develop fixed asset management system
Bubble sort array
End to end object detection with transformers (Detr) paper reading and understanding
Juypter notebook modify the default open folder and default browser
安装单机redis详细教程
Usage of ieda refactor
Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3
随机推荐
When converting from list to map, if a certain attribute may cause key duplication and exceptions, you can set the way to deal with this duplication
移动机器人路径规划:人工势场法[通俗易懂]
Preprocessing and preprocessing macros
Codeworks round 802 (Div. 2) pure supplementary questions
A4988 drive stepper motor "recommended collection"
451-memcpy、memmove、memset的实现
使用 Cheat Engine 修改 Kingdom Rush 中的金钱、生命、星
AcWing 1135. 新年好 题解(最短路+搜索)
Implementation of 452 strcpy, strcat, StrCmp, strstr, strchr
Use cheat engine to modify money, life and stars in Kingdom rush
In pytorch function__ call__ And forward functions
开发固定资产管理系统,开发固定资产管理系统用什么语音
metric_ Logger urination
MySQL advanced (Advanced) SQL statement
Golang concurrent programming goroutine, channel, sync
Markdown基础语法
Excel finds the same value in a column, deletes the row or replaces it with a blank value
High frequency interview questions
4274. 后缀表达式-二叉表达式树
AcWing 383. 观光 题解(最短路)