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

推荐订阅源

Attack and Defense Labs
Attack and Defense Labs
The GitHub Blog
The GitHub Blog
C
Check Point Blog
博客园_首页
MongoDB | Blog
MongoDB | Blog
N
Netflix TechBlog - Medium
F
Full Disclosure
Microsoft Security Blog
Microsoft Security Blog
爱范儿
爱范儿
Recent Announcements
Recent Announcements
阮一峰的网络日志
阮一峰的网络日志
G
GRAHAM CLULEY
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Threat Research - Cisco Blogs
C
Cybersecurity and Infrastructure Security Agency CISA
V
Vulnerabilities – Threatpost
K
Kaspersky official blog
博客园 - 司徒正美
S
Schneier on Security
T
The Exploit Database - CXSecurity.com
Project Zero
Project Zero
云风的 BLOG
云风的 BLOG
Cisco Talos Blog
Cisco Talos Blog
Know Your Adversary
Know Your Adversary
雷峰网
雷峰网
V
V2EX - 技术
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Spread Privacy
Spread Privacy
罗磊的独立博客
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
S
Security Affairs
SecWiki News
SecWiki News
Schneier on Security
Schneier on Security
O
OpenAI News
Jina AI
Jina AI
PCI Perspectives
PCI Perspectives
Cyberwarzone
Cyberwarzone
Y
Y Combinator Blog
Apple Machine Learning Research
Apple Machine Learning Research
B
Blog RSS Feed
I
InfoQ
D
Docker
P
Palo Alto Networks Blog
Recorded Future
Recorded Future
M
MIT News - Artificial intelligence
博客园 - Franky
B
Blog
Scott Helme
Scott Helme
博客园 - 叶小钗
D
DataBreaches.Net

博客园 - zhaowt001

HTML 转 PDF Sort using in VS C#、ASP.NET获取当前应用程序的绝对路径,获取程序工作路径 (转帖) 炒股经验 完美解读Linux中文件系统的目录结构 30个优秀.net在线学习资源站点 如何删除windows service(转帖) Windows CE 电源管理(转贴) C#中获取程序当前路径的集中方法 随心所欲操作Enum枚举类型 (转贴) 远程连接SQL Server 2000服务器的解决方案 对软件的新认识 EVENT IIS 配置 - zhaowt001 - 博客园 一个程序员成长的六个阶段 优秀程序员应当具备的品质 ASP.NET的网站的设计与优化 解决IIS服务器Web访问提示输入密码 山东人!
override new 关键字的区别
zhaowt001 · 2008-11-21 · via 博客园 - zhaowt001

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OverrideTest
{
    class Program
    {
        static void Main(string[] args)
        {
            A A = new A();
            B B = new B();
            C C = new C();
            D D = new D();

            A a = new A();
            A b = new B();
            A c = new C();
            A d = new D();

            B BC = new C();
            B BD = new D();
            C CD = new D();

            A AB = (A)B;
            A AC = (A)C;
            A AD = (A)D;

            Console.WriteLine("A =" + a.mode());
            Console.WriteLine("B =" + b.mode());
            Console.WriteLine("C =" + c.mode());
            Console.WriteLine("D =" + d.mode());
            Console.WriteLine("A =" + A.mode());
            Console.WriteLine("B =" + B.mode());
            Console.WriteLine("C =" + C.mode());
            Console.WriteLine("D =" + D.mode());
            Console.WriteLine("BC =" + BC.mode());
            Console.WriteLine("BD =" + BD.mode());
            Console.WriteLine("CD =" + CD.mode());
            Console.WriteLine("AD =" + AD.mode());
            Console.WriteLine("AB =" + AB.mode());
            Console.WriteLine("AC =" + AC.mode());
            Console.ReadLine();
        }
    }

    public class A
    {
        virtual public string mode()
        {
            return "A";
        }
    }
    public class B : A
    {
        public override string mode()
        {
            return "B";
        }
    }
    public class C : B
    {
        public virtual new string mode()
        {
            return "C";
        }
    }
    public class D : C
    {
        public override string mode()
        {
            return "D";
        }
    }


}

/*answer
 *
A =A
B =B
C =B
D =B
A =A
B =B
C =C
D =D
BC =B
BD =B
CD =D
AD =B
AB =B
AC =B
 *
 *
 */

C# 语言经过专门设计,以便不同库中的基类与派生类之间的版本控制可以不断向前发展,同时保持向后兼容。这具有多方面的意义。例如,这意味着在基类中引入与派生类中的某个成员具有相同名称的新成员在 C# 中是完全支持的,不会导致意外行为。它还意味着类必须显式声明某方法是要重写一个继承方法,还是一个隐藏具有类似名称的继承方法的新方法。

在 C# 中,派生类可以包含与基类方法同名的方法。

  • 基类方法必须定义为 virtual。

  • 如果派生类中的方法前面没有 new 或 override 关键字,则编译器将发出警告,该方法将有如存在 new 关键字一样执行操作。

  • 如果派生类中的方法前面带有 new 关键字,则该方法被定义为独立于基类中的方法。

  • 如果派生类中的方法前面带有 override 关键字,则派生类的对象将调用该方法,而不是调用基类方法。

  • 可以从派生类中使用 base 关键字调用基类方法。

  • overridevirtualnew 关键字还可以用于属性、索引器和事件中。

默认情况下,C# 方法为非虚方法。如果某个方法被声明为虚方法,则继承该方法的任何类都可以实现它自己的版本。若要使方法成为虚方法,必须在基类的方法声明中使用 virtual 修饰符。然后,派生类可以使用 override 关键字重写基虚方法,或使用 new 关键字隐藏基类中的虚方法。如果 override 关键字和 new 关键字均未指定,编译器将发出警告,并且派生类中的方法将隐藏基类中的方法。