当前位置:网站首页>多态的理解以及作用
多态的理解以及作用
2022-07-02 18:23:00 【夏诗曼CharmaineXia】
一、多态是什么
多态在面向对象中的定义:指为不同数据类型的实体提供统一的接口。
比如有动物(Animal)之类别(Class),而且由动物继承出类别鸡(Chicken)和类别狗(Dog),并对同一源自类别动物(父类)之一消息有不同的响应,如类别动物有“叫()”之动作,而类别鸡会“啼叫()”,类别狗则会“吠叫()”,则称之为多态。
二、多态的类型
多态分为静态多态和动态多态。
- 静态多态:在编译期间实现多态,相同的方法传递不同的参数,实现不同的效果。
C++代码实现:
// print()函数
void print(const char* str,int width);
void print(double i ,int width);
void print(const char* str);
// 使用print()函数时,编译器将根据所采取的用法使用有相应特征标的原型
print("abc",12);
print(2.2,55);
print("def");
- 动态多态:在运行期间实现多态,一个类的引用可以被赋值为不同的子类对象。
实现动态多态的必要条件:
- 编程语言要支持继承
- 子类可以重写(override)父类中的方法
- 父类引用可以指向子类对象
C#代码实现:
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;
}
}
}
三、多态解决什么问题
- 应用程序不必为每一个子类编写功能调用,只需要对父类进行处理即可。大大提高程序的可复用性。
- 子类的功能可以被父类的方法或引用变量所调用,就像Animal类,可以提高可扩充性和可维护性。
边栏推荐
- Emmet基础语法
- Data dimensionality reduction factor analysis
- PHP非对称加密方法私钥及公钥加密解密的方法
- LeetCode 0871.最低加油次数 - 类似于POJ2431丛林探险
- End-to-End Object Detection with Transformers(DETR)论文阅读与理解
- Preprocessing and preprocessing macros
- 横向越权与纵向越权[通俗易懂]
- [paper reading] Ca net: leveraging contextual features for lung cancer prediction
- 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
- #gStore-weekly | gStore源码解析(四):安全机制之黑白名单配置解析
猜你喜欢
高级性能测试系列《24. 通过jdbc执行sql脚本》
End-to-End Object Detection with Transformers(DETR)论文阅读与理解
How performance testing creates business value
xml开发方式下AutowiredAnnotationBeanPostProcessor的注册时机
Machine learning notes - time series prediction research: monthly sales of French champagne
Excel finds the same value in a column, deletes the row or replaces it with a blank value
使用CLion编译OGLPG-9th-Edition源码
程序猿入门攻略(十二)——数据的存储
Excel查找一列中的相同值,删除该行或替换为空值
【测试开发】软件测试—概念篇
随机推荐
2022.7.1-----leetcode. two hundred and forty-one
MySQL高级(进阶)SQL语句
Usage of ieda refactor
A4988 drive stepper motor "recommended collection"
Obligatoire pour les débutants, cliquez sur deux boutons pour passer à un contenu différent
Novice must see, click two buttons to switch to different content
Npoi export Excel2007
[pytorch learning notes] tensor
C文件输入操作
Gmapping code analysis [easy to understand]
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
Use cheat engine to modify money, life and stars in Kingdom rush
IDEA编辑器去掉sql语句背景颜色SQL语句警告No data sources are configured to run this SQL...和SQL Dialect is Not Config
二进制操作
[error record] problems related to the installation of the shuttle environment (follow-up error handling after executing the shuttle doctor command)
GMapping代码解析[通俗易懂]
Golang concurrent programming goroutine, channel, sync
Fastdfs installation
ORA-01455: converting column overflows integer datatype
Juypter notebook modify the default open folder and default browser