当前位置:网站首页>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 .
边栏推荐
- 教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 网络安全专家 NSE 5
- 高级性能测试系列《24. 通过jdbc执行sql脚本》
- 使用xml文件打印mybaties-log插件的方式
- GMapping代码解析[通俗易懂]
- 线程应用实例
- Typescript 之 快速入门
- AcWing 181. 回转游戏 题解(搜索—IDA*搜索)
- Tutorial (5.0) 09 Restful API * fortiedr * Fortinet network security expert NSE 5
- Develop fixed asset management system, what voice is used to develop fixed asset management system
- Gamefi链游系统开发(NFT链游开发功能)丨NFT链游系统开发(Gamefi链游开发源码)
猜你喜欢

In pytorch function__ call__ And forward functions

AcWing 903. 昂贵的聘礼 题解(最短路—建图、dijkstra)

Excel finds the same value in a column, deletes the row or replaces it with a blank value

Markdown基础语法

End to end object detection with transformers (Detr) paper reading and understanding

云呐|为什么要用固定资产管理系统,怎么启用固定资产管理系统

Chic Lang: completely solve the problem of markdown pictures - no need to upload pictures - no need to network - there is no lack of pictures forwarded to others

Tutoriel (5.0) 10. Dépannage * fortiedr * fortinet Network Security expert NSE 5

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

Registration opportunity of autowiredannotationbeanpostprocessor in XML development mode
随机推荐
AcWing 1129. 热浪 题解(最短路—spfa)
codeforces每日5题(均1700)-第四天
Gmapping code analysis [easy to understand]
程序猿入门攻略(十二)——数据的存储
开发固定资产管理系统,开发固定资产管理系统用什么语音
The mybatieshelperpro tool can be generated to the corresponding project folder if necessary
Machine learning notes - time series prediction research: monthly sales of French champagne
Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3
"Patient's family, please come here" reading notes
Yunna | why use the fixed asset management system and how to enable it
数据降维——因子分析
MySQL
AcWing 1137. 选择最佳线路 题解(最短路)
Golang concurrent programming goroutine, channel, sync
Notes de lecture sur le code propre
2022.7.1-----leetcode. two hundred and forty-one
AcWing 343. 排序 题解(floyd性质实现传递闭包)
AcWing 1128. 信使 题解(最短路—Floyd)
Getting started with typescript
第七章-类基础