当前位置:网站首页>静态方法与实例方法的区别
静态方法与实例方法的区别
2022-07-21 11:24:00 【ca1man】
静态方法与实例方法的区别
public class Parent
{
public void Method_A()
{
Console.WriteLine("Parent Method_A");
}
public virtual void Method_B()
{
Console.WriteLine("Parent Method_B");
}
}
public class Child : Parent
{
public new void Method_A()
{
Console.WriteLine("Child Method_A");
}
public override void Method_B()
{
Console.WriteLine("Child Method_B");
}
}
class Program
{
static void Main(string[] args)
{
Child child = new Child();
child.Method_A();
child.Method_B();
Parent p = child as Parent;
p.Method_A();
p.Method_B();
Console.Read();
}
}
显示结果
总结
1、子类使用new隐藏父类方法后,是独立于父类的方法。
2、子类使用override重写父类的方法后,只是在调用时不再调用父类方法而调用子类方法。
3、子类也可以使用new隐藏父类的虚方法。
4、创建的Child实例,在执行Method_A和Method_B方法时,都执行的是Child中的方法。
但是将该实例转换成父类时,Method_A执行的是Parent的Method_A方法,而Method_B执行的依旧是Child中的Method_B方法。
边栏推荐
- SCA在得物DevSecOps平台上应用
- 网站为什么会有漏洞要去修补
- "F5g+eiot" build the energy Internet of things and help the data service of the power Internet of things
- PostgreSQL每周新闻—2022年7月13日
- 解决ssh登录后闲置时间过长而断开连接
- Talking about the application of service worker in caching resources and web push
- DDR SDRAM板卡设计指南
- PostgreSQL初/中/高级认证考试(7.16)通过考生公示
- Oneinstack安装与配置PHP 8.1和MySQL 8.0-Oneinstack建站新手教程
- MySQL(数据类型和完整约束)
猜你喜欢
随机推荐
一招教你拿捏网上视频
When servlet writes webapp, filter interception is used to realize login verification
抽象类名作为形参和返回值
MySQL表与表之间的关系详解
A solution for adding new MySQL users that cannot be authorized
Idea reports an error for help!!!!
Talking about the application of service worker in caching resources and web push
Idea建文件夹时,文件夹的空文件夹的展开与重叠
对图的广度优先遍历BFS和深度优先遍历DFS的应用——基于LeetCode133 克隆图
如何搭建一个好的知识库管理系统?
MySQL终章
PCI Express物理层 - 电气部分
MONAI Label -- 使用 AI 加速你的分割标注
JS部分
Cadence OrCAD Capture TCL/TK脚本实例
生物制药安全供电保生产
湘潭市党政代表团莅临麒麟信安调研考察
MONAI Label 安装流程及使用攻略
Condition judgment order of inexplicable out of bounds error causes -- Based on leetcode 99 question, restore binary search tree
企业进行知识管理有哪些好处?