当前位置:网站首页>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 .
边栏推荐
- Use cheat engine to modify money, life and stars in Kingdom rush
- [pytorch learning notes] tensor
- Markdown基础语法
- Golang concurrent programming goroutine, channel, sync
- MySQL高级(进阶)SQL语句
- 《代码整洁之道》读书笔记
- xml开发方式下AutowiredAnnotationBeanPostProcessor的注册时机
- Yolov3 trains its own data set to generate train txt
- mysql函数
- AcWing 1125. 牛的旅行 题解(最短路、直径)
猜你喜欢
Compile oglpg-9th-edition source code with clion
Juypter notebook modify the default open folder and default browser
开发固定资产管理系统,开发固定资产管理系统用什么语音
End-to-End Object Detection with Transformers(DETR)论文阅读与理解
Windows2008R2 安装 PHP7.4.30 必须 LocalSystem 启动应用程序池 不然500错误 FastCGI 进程意外退出
Watchful pioneer world outlook Architecture - (how does a good game come from)
Reading notes of "the way to clean structure" (Part 2)
IDEA编辑器去掉sql语句背景颜色SQL语句警告No data sources are configured to run this SQL...和SQL Dialect is Not Config
Bubble sort array
注解开发方式下AutowiredAnnotationBeanPostProcessor的注册时机
随机推荐
Tutorial (5.0) 09 Restful API * fortiedr * Fortinet network security expert NSE 5
MySQL表历史数据清理总结
Emmet基础语法
"Patient's family, please come here" reading notes
Binary operation
第七章-类基础
metric_ Logger urination
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
Preprocessing and preprocessing macros
Codeworks round 802 (Div. 2) pure supplementary questions
Golang并发编程——goroutine、channel、sync
股票证券公司排名,有安全保障吗
横向越权与纵向越权[通俗易懂]
Compile oglpg-9th-edition source code with clion
GMapping代码解析[通俗易懂]
电脑使用哪个录制视频软件比较好
What is 9D movie like? (+ common sense of dimension space)
Why should we build an enterprise fixed asset management system and how can enterprises strengthen fixed asset management
数据湖(十二):Spark3.1.2与Iceberg0.12.1整合
Reading notes of "the way to clean structure" (Part 2)