当前位置:网站首页>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 .
边栏推荐
- Pytorch版本、CUDA版本与显卡驱动版本的对应关系
- Py之interpret:interpret的简介、安装、案例应用之详细攻略
- 数据湖(十二):Spark3.1.2与Iceberg0.12.1整合
- 2022 compilation principle final examination recall Edition
- Gmapping code analysis [easy to understand]
- Watchful pioneer world outlook Architecture - (how does a good game come from)
- 移动机器人路径规划:人工势场法[通俗易懂]
- 453-atoi函数的实现
- AcWing 342. Road and route problem solving (shortest path, topological sorting)
- 冒泡排序数组
猜你喜欢

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

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

开发固定资产管理系统,开发固定资产管理系统用什么语音

Usage of ieda refactor

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

Registration opportunity of autowiredannotationbeanpostprocessor under annotation development mode

codeforces每日5题(均1700)-第四天

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

Markdown basic grammar

PHP-Parser羽毛球预约小程序开发require线上系统
随机推荐
In pytorch function__ call__ And forward functions
Registration opportunity of autowiredannotationbeanpostprocessor in XML development mode
PHP parser badminton reservation applet development requires online system
Emmet基础语法
Golang:[]byte to string
metric_ Logger urination
【ERP软件】ERP体系二次开发有哪些危险?
AcWing 1125. 牛的旅行 题解(最短路、直径)
xml开发方式下AutowiredAnnotationBeanPostProcessor的注册时机
mysql备份后缀是什么_mysql备份还原
Codeworks 5 questions per day (1700 average) - day 4
AcWing 1129. 热浪 题解(最短路—spfa)
Juypter notebook modify the default open folder and default browser
PHP非对称加密方法私钥及公钥加密解密的方法
Golang concurrent programming goroutine, channel, sync
Data dimensionality reduction principal component analysis
Function high order curry realization
Reading notes of code neatness
Tutorial (5.0) 10 Troubleshooting * fortiedr * Fortinet network security expert NSE 5
Usage of ieda refactor