当前位置:网站首页>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 .
边栏推荐
- Data dimensionality reduction principal component analysis
- 2022.7.1-----leetcode. two hundred and forty-one
- Digital scroll strip animation
- Fastdfs installation
- Notes de lecture sur le code propre
- 450-深信服面经1
- Function high order curry realization
- MySQL表历史数据清理总结
- AcWing 383. Sightseeing problem solution (shortest circuit)
- Emmet基础语法
猜你喜欢

Tutorial (5.0) 09 Restful API * fortiedr * Fortinet network security expert NSE 5

Windows2008r2 installing php7.4.30 requires localsystem to start the application pool, otherwise 500 error fastcgi process exits unexpectedly

全志A33使用主线U-Boot

End-to-End Object Detection with Transformers(DETR)论文阅读与理解

潇洒郎:彻底解决Markdown图片问题——无需上传图片——无需网络——转发给他人图片无缺失

What is 9D movie like? (+ common sense of dimension space)

zabbix5客户端安装和配置

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

数据降维——因子分析

搭建主从模式集群redis
随机推荐
教程篇(5.0) 09. RESTful API * FortiEDR * Fortinet 网络安全专家 NSE 5
多态的理解以及作用
LeetCode 0871. Minimum refueling times - similar to poj2431 jungle adventure
安装单机redis详细教程
Windows2008R2 安装 PHP7.4.30 必须 LocalSystem 启动应用程序池 不然500错误 FastCGI 进程意外退出
IEDA refactor的用法
AcWing 342. 道路与航线 题解 (最短路、拓扑排序)
Introduction of Ethernet PHY layer chip lan8720a
Memory management of C
使用 Cheat Engine 修改 Kingdom Rush 中的金钱、生命、星
Emmet basic syntax
函数高阶-柯里化实现
Data dimensionality reduction factor analysis
451-memcpy、memmove、memset的实现
《重构:改善既有代码的设计》读书笔记(下)
使用xml文件打印mybaties-log插件的方式
第七章-类基础
Codeforces Round #802 (Div. 2) 纯补题
IDEA编辑器去掉sql语句背景颜色SQL语句警告No data sources are configured to run this SQL...和SQL Dialect is Not Config
AcWing 342. Road and route problem solving (shortest path, topological sorting)