惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
T
Threatpost
Latest news
Latest news
N
News | PayPal Newsroom
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Help Net Security
Help Net Security
D
Darknet – Hacking Tools, Hacker News & Cyber Security
AI
AI
Simon Willison's Weblog
Simon Willison's Weblog
TaoSecurity Blog
TaoSecurity Blog
The Last Watchdog
The Last Watchdog
L
LINUX DO - 热门话题
Google DeepMind News
Google DeepMind News
T
Threat Research - Cisco Blogs
O
OpenAI News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
The Exploit Database - CXSecurity.com
NISL@THU
NISL@THU
Application and Cybersecurity Blog
Application and Cybersecurity Blog
S
Securelist
小众软件
小众软件
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Martin Fowler
Martin Fowler
S
SegmentFault 最新的问题
Cisco Talos Blog
Cisco Talos Blog
云风的 BLOG
云风的 BLOG
AWS News Blog
AWS News Blog
GbyAI
GbyAI
N
News and Events Feed by Topic
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
美团技术团队
Engineering at Meta
Engineering at Meta
A
About on SuperTechFans
博客园 - 三生石上(FineUI控件)
S
Schneier on Security
博客园 - 聂微东
V2EX - 技术
V2EX - 技术
T
Troy Hunt's Blog
SecWiki News
SecWiki News
S
Secure Thoughts
B
Blog RSS Feed
Hugging Face - Blog
Hugging Face - Blog
WordPress大学
WordPress大学
腾讯CDC
H
Heimdal Security Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Apple Machine Learning Research
Apple Machine Learning Research
月光博客
月光博客
www.infosecurity-magazine.com
www.infosecurity-magazine.com
P
Privacy International News Feed

博客园 - 山峰旺旺

博文阅读密码验证 - 博客园 2019年春节第一天上班 FastDFS搭建文件系统(单机版) 博文阅读密码验证 - 博客园 CentOS7 下docker 部署 Asp.Net Core Linux (CentOS7.0)安装Asp.Net Core项目总结 Linux 下 安装 .Net Core(CentOS 7) Spring框架IOC容器和AOP解析(转) 文本相似度simhash算法 Intellij Idea 2017.3版本MAVEN项目打JAR包 java 敏感词过滤 (DFA算法)(转) intellij idea 修改背景保护色&&修改字体&&快捷键大全(转) C#队列Queue实现一个简单的电商网站秒杀程序 2018年目标和愿景 CDN原理(转,学习用) 面试理论整理 C#中的where泛型约束中的new()使用(转) C# unsafe(fixed) 介于 managed code & unmanaged code之间的特性(转) 2015年总结
C#中重写(override)和覆盖(new)的区别 (备注:转,留自己用)
山峰旺旺 · 2016-06-02 · via 博客园 - 山峰旺旺

重写

用关键字 virtual 修饰的方法,叫虚方法。可以在子类中用override 声明同名的方法,这叫“重写”。相应的没有用virtual修饰的方法,我们叫它实方法。 重写会改变父类方法的功能。 看下面演示代码:

复制代码

#region 重写
public class C1 {     public virtual string GetName()     {         return "徐明祥";     } }
public class C2 : C1 {     public override string GetName()     {         return "xumingxiang";     } }
 C1 c1 = new C1();  Console.WriteLine(c1.GetName());//输出“徐明祥”  C2 c2 = new C2();  Console.WriteLine(c2.GetName());//输出“xumingxiang”

 //重点看这里

 C1 c3 = new C2();  Console.WriteLine(c3.GetName());//输出“xumingxiang”  #endregion

复制代码

覆盖 在子类中用 new 关键字修饰 定义的与父类中同名的方法,叫覆盖。

覆盖不会改变父类方法的功能。

看下面演示代码:

复制代码

#region 覆盖
public class C1 {     public string GetName()     {         return "徐明祥";     } }
public class C2 : C1 {     public new string GetName()     {         return "xumingxiang";     } }
C1 c1 = new C1(); Console.WriteLine(c1.GetName());//输出“徐明祥” C2 c2 = new C2(); Console.WriteLine(c2.GetName());//输出“xumingxiang”

//重点看这里,和上面的重写作比较

C1 c3 = new C2(); Console.WriteLine(c3.GetName());//输出“徐明祥”  #endregion

复制代码

 注:重载方法不同啊

 1:方法在同一个类中;

 2:方法名相同,参数列表不同;

 3:方法重载不需要关键字;

总结

1:不管是重写还是覆盖都不会影响父类自身的功能。

2:当用子类创建父类的时候,如 C1 c3 = new C2(),重写会改变父类的功能,即调用子类的功能;而覆盖不会,仍然调用父类功能。

3:虚方法(virtual)、实方法都可以被覆盖(new),抽象方法abstract,接口interface 不可以。

4:抽象方法,接口,虚方法都可以被重写(override),实方法不可以。

5:重写使用的频率比较高,实现多态;覆盖用的频率比较低,用于对以前无法修改的类进行继承的时候。