当前位置:网站首页>Understanding polymorphism
Understanding polymorphism
2022-06-30 21:01:00 【Sainan Muzi Xiaomiao】
If you write a piece of code , Reflect polymorphism , How would you write ?
How do you define polymorphism , There are probably several different explanations :
1、 The ability of a behavior to have multiple forms or forms of expression . Polymorphism based on inheritance .
2、 It refers to providing a unified interface for entities of different data types .
3、 Use the reference of the parent class to point to the object of the child class .
If I choose without thinking, I will choose the first answer , But after careful consideration, I chose the third explanation . Let's start with a piece of code
public static void Main(string[] args)
{
Operation oper;
oper = OperationFactory.createOperate("+");
oper.NumberA = 1;
oper.NumberB = 2;
double result=oper.GetResult();
Console.WriteLine(" The result is :"+result);
Console.ReadKey();
} class OperationAdd:Operation
{
public override double GetResult()
{
double result = 0;
result = NumberA + NumberB;
return result;
}
}class OperationMul : Operation
{
public override double GetResult()
{
double result = 0;
result = NumberA * NumberB;
return result;
}
}The following two codes show that addition and multiplication are inherited from Operation,Operation Only attributes and a virtual method are defined .
public class Operation
{// Define two variables , Two attributes , A virtual method
private double _numberA = 0;
private double _numberB = 0;
public double NumberA
{
get { return _numberA; }
set { _numberA = value; }
}
public double NumberB
{
get { return _numberB; }
set { _numberB = value; }
}
public virtual double GetResult()
{
double result = 0;
return result;
}
}
}From the code above , What can best reflect polymorphism is the following sentence
oper = OperationFactory.createOperate("+");Let me talk about my understanding :
Why the parent class reference points to the child class object reflects polymorphism ?
What is declared is the parent object , But when compiling, it is a subclass object , When a subclass is instantiated, it will compile the parent class as well !
When calling a method , After the parent class declaration is forcibly converted to a child class object , Is a method that can be called to subclasses , So the concrete implementation is the subclass method , The same behavior is realized ( Method ) The ability to have many different manifestations or forms . In fact, the use of polymorphism , Not just to extend the functionality of the parent class , But by overriding the parent method , Override or add subclass specific functions , To achieve the implementation method of calling the method of the parent class and finally pointing to the specific subclass .
I wonder if I have the same question , The above code seems to reflect the Richter replacement principle more , Is there any difference between polymorphism and Richter substitution ?
The polymorphism mentioned above is more about rewriting the parent class method , Override or add subclass specific functions . And the use of Richter substitution , More is to extend the function of the parent class , But not inclined to change the original function of the parent class , Just add some new methods .
边栏推荐
猜你喜欢

Amazon restricts LGBTQ related search and product sales in the United Arab Emirates

浅谈代码语言的魅力

B_QuRT_User_Guide(33)

MySQL:SQL概述及数据库系统介绍 | 黑马程序员

Lumiprobe 聚乙二醇化和 PEG 接头丨碘-PEG3-酸研究

注册设备监理师难考吗,和监理工程师有什么关系?

哈夫曼樹(一)基本概念與C語言實現

Lumiprobe细胞生物学丨DiA,亲脂性示踪剂说明书

A complete collection of vulnerability scanning tools. Mom doesn't have to worry that I won't find any more vulnerabilities

Basic concepts of tree
随机推荐
Web APIs 综合案例-Tab栏切换 丨黑马程序员
Wechat applet development practice cloud music
Lumiprobe染料 NHS 酯丨BDP FL NHS 酯研究
Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedded datasource could
文本生成模型退化怎么办?SimCTG 告诉你答案
阿里kube-eventer mysql sink简单使用记录
oprator-1初识oprator
Lumiprobe nucleic acid quantitative qudye dsDNA br detection kit
uniapp-路由uni-simple-router
Peking University ACM problems 1005:i think I need a houseboat
毕业设计
DM8:生成DM AWR报告
Go learning notes
微信小程序怎么实现圆心进度条
开发技术-获取10分钟前的时间
申请Vector 总线协议彩图壁纸挂画,非常棒哦!
关于,奇安信检测代码漏洞,XSS系列解决
ncat详细介绍(转载)
B_QuRT_User_Guide(32)
How do I get the largest K massive data