当前位置:网站首页>多态的理解以及作用
多态的理解以及作用
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类,可以提高可扩充性和可维护性。
边栏推荐
- Refactoring: improving the design of existing code (Part 1)
- Imitation Jingdong magnifying glass effect (pink teacher version)
- How performance testing creates business value
- Tutoriel (5.0) 10. Dépannage * fortiedr * fortinet Network Security expert NSE 5
- PyTorch函数中的__call__和forward函数
- 使用xml文件打印mybaties-log插件的方式
- When converting from list to map, if a certain attribute may cause key duplication and exceptions, you can set the way to deal with this duplication
- zabbix5客户端安装和配置
- 450-深信服面经1
- [pytorch learning notes] tensor
猜你喜欢

Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3

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

xml开发方式下AutowiredAnnotationBeanPostProcessor的注册时机

PHP parser badminton reservation applet development requires online system

Imitation Jingdong magnifying glass effect (pink teacher version)

High frequency interview questions

Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径

搭建主从模式集群redis

仿京东放大镜效果(pink老师版)
![[error record] problems related to the installation of the shuttle environment (follow-up error handling after executing the shuttle doctor command)](/img/c1/a00425f2e1824a50644c8fbb15fe38.jpg)
[error record] problems related to the installation of the shuttle environment (follow-up error handling after executing the shuttle doctor command)
随机推荐
【测试开发】软件测试—概念篇
Tutorial (5.0) 10 Troubleshooting * fortiedr * Fortinet network security expert NSE 5
IEDA refactor的用法
线程应用实例
虚拟机初始化脚本, 虚拟机相互免秘钥
使用xml文件打印mybaties-log插件的方式
Tutorial (5.0) 09 Restful API * fortiedr * Fortinet network security expert NSE 5
[test development] takes you to know what software testing is
When converting from list to map, if a certain attribute may cause key duplication and exceptions, you can set the way to deal with this duplication
Npoi export Excel2007
Emmet basic syntax
Thread application instance
mybatiesHelperPro工具必须的可以生成到对应项目文件夹下
ORA-01455: converting column overflows integer datatype
[0701] [论文阅读] Alleviating Data Imbalance Issue with Perturbed Input During Inference
High frequency interview questions
Golang:[]byte to string
Learn the knowledge points of eight part essay ~ ~ 1
数字滚动带动画
Markdown基础语法