当前位置:网站首页>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 .
边栏推荐
猜你喜欢

申请Vector 总线协议彩图壁纸挂画,非常棒哦!

Basic concepts of tree

ArcGIS构建发布简单路网Network数据服务及Rest调用测试

SQL Server 提取字符串中的纯数字

MySQL高级篇3

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

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

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

哈夫曼树(一)基本概念与C语言实现

Lumiprobe生物素亚磷酰胺(羟脯氨酸)说明书
随机推荐
[1175. prime number arrangement]
Wechat applet development practice cloud music
RP原型资源分享-购物类App
sqlserver 字符串类型转换成小数或者整数类型
DM8:生成DM AWR报告
减少嵌入式软件调试时间的三个技巧
Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedded datasource could
翻转链表II[翻转链表3种方式+dummyHead/头插法/尾插法]
【微服务~Nacos】Nacos之配置中心
What bank card do you need to open an account online? In addition, is it safe to open an account online now?
关于,奇安信检测代码漏洞,XSS系列解决
Peking University ACM problems 1002:487-3279
Amazon restricts LGBTQ related search and product sales in the United Arab Emirates
Go learning notes
等级测评是什么意思?工作流程包含哪些?
将博客搬至CSDN
Lumiprobe 改性三磷酸盐丨生物素-11-UTP研究
树基本概念
有趣网站汇总
C file pointer