当前位置:网站首页>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 .
边栏推荐
- How to print mybats log plug-in using XML file
- Virtual machine initialization script, virtual machine mutual secret key free
- Why should we build an enterprise fixed asset management system and how can enterprises strengthen fixed asset management
- Bubble sort array
- Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3
- AcWing 1126. 最小花费 题解(最短路—dijkstra)
- 453-atoi函数的实现
- Yolov3 trains its own data set to generate train txt
- Gamefi chain game system development (NFT chain game development function) NFT chain game system development (gamefi chain game development source code)
- Typescript 之 快速入门
猜你喜欢
Bubble sort array

Tutorial (5.0) 10 Troubleshooting * fortiedr * Fortinet network security expert NSE 5

zabbix5客户端安装和配置

PHP-Parser羽毛球预约小程序开发require线上系统

高级性能测试系列《24. 通过jdbc执行sql脚本》

Registration opportunity of autowiredannotationbeanpostprocessor under annotation development mode

《重构:改善既有代码的设计》读书笔记(上)

Yolov3 trains its own data set to generate train txt

Compile oglpg-9th-edition source code with clion

codeforces每日5题(均1700)-第四天
随机推荐
Quanzhi A33 uses mainline u-boot
mysql函数
Watchful pioneer world outlook Architecture - (how does a good game come from)
Codeworks 5 questions per day (1700 average) - day 4
Data dimensionality reduction factor analysis
安装单机redis详细教程
[pytorch learning notes] tensor
AcWing 1127. 香甜的黄油 题解(最短路—spfa)
According to the atlas of data security products and services issued by the China Academy of information technology, meichuang technology has achieved full coverage of four major sectors
Gmapping code analysis [easy to understand]
搭建哨兵模式reids、redis从节点脱离哨兵集群
AcWing 341. 最优贸易 题解 (最短路、dp)
Registration opportunity of autowiredannotationbeanpostprocessor in XML development mode
教程篇(5.0) 09. RESTful API * FortiEDR * Fortinet 网络安全专家 NSE 5
AcWing 1128. 信使 题解(最短路—Floyd)
【ERP软件】ERP体系二次开发有哪些危险?
Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3
"Patient's family, please come here" reading notes
守望先锋世界观架构 ——(一款好的游戏是怎么来的)
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