当前位置:网站首页>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 .
边栏推荐
- How do I get the largest K massive data
- A complete collection of vulnerability scanning tools. Mom doesn't have to worry that I won't find any more vulnerabilities
- On the charm of code language
- Analysis of breakpoint continuation and download principle
- 二叉查找树(一) - 概念与C语言实现
- jfinal中如何使用过滤器监控Druid监听SQL执行?
- Evolution of screen display technology
- Lumiprobe染料酰肼丨BDP FL 酰肼方案
- Lumiprobe蛋白质定量丨QuDye 蛋白定量试剂盒
- No "history of blood and tears" in home office | community essay solicitation
猜你喜欢
注册设备监理师难考吗,和监理工程师有什么关系?
Lumiprobe nucleic acid quantitative qudye dsDNA br detection kit
Software engineering UML drawing
Binary search tree (1) - concept and C language implementation
个人开发的渗透测试工具Satania
Basic components of STL
Lumiprobe protein quantitation - qudye Protein Quantitation Kit
Lumiprobe染料 NHS 酯丨BDP FL NHS 酯研究
科研中遇到迷茫困惑如何向前一步?如何在科研中发挥女性优势?
uniapp怎么上传二进制图片
随机推荐
mysql-批量更新
ICML2022 | 序列决策的效用理论
申请Vector 总线协议彩图壁纸挂画,非常棒哦!
Binary search tree (1) - concept and C language implementation
uniapp怎么上传二进制图片
vncserver: Failed command ‘/etc/X11/Xvnc-session‘: 256!
AVL平衡二叉树(一) - 概念与C语言实现
三个火枪手
SqlServer 获取字符串中数字,中文及字符部分数据
在手机炒股开户安全吗?
No "history of blood and tears" in home office | community essay solicitation
On inline function
Huffman tree (I) basic concept and C language implementation
Wechat applet development practice cloud music
19.04 distributor
Study on lumiprobe dye NHS ester BDP FL NHS ester
What are database OLAP and OLTP? Same and different? Applicable scenarios
Learning summary
centos——开启/关闭oracle
报错:Internal error XFS_WANT_CORRUPTED_GOTO at line 1635 of file fs/xfs/libxfs/xfs_alloc.c.