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

推荐订阅源

C
CXSECURITY Database RSS Feed - CXSecurity.com
P
Privacy International News Feed
V
Vulnerabilities – Threatpost
The Last Watchdog
The Last Watchdog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
O
OpenAI News
T
Threat Research - Cisco Blogs
WordPress大学
WordPress大学
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
P
Palo Alto Networks Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
H
Help Net Security
P
Proofpoint News Feed
MyScale Blog
MyScale Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
T
The Blog of Author Tim Ferriss
H
Hackread – Cybersecurity News, Data Breaches, AI and More
S
Securelist
Vercel News
Vercel News
S
Security Affairs
D
Darknet – Hacking Tools, Hacker News & Cyber Security
B
Blog RSS Feed
云风的 BLOG
云风的 BLOG
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Blog — PlanetScale
Blog — PlanetScale
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Last Week in AI
Last Week in AI
博客园_首页
Attack and Defense Labs
Attack and Defense Labs
G
Google Developers Blog
T
Tor Project blog
Project Zero
Project Zero
腾讯CDC
Schneier on Security
Schneier on Security
月光博客
月光博客
N
Netflix TechBlog - Medium
AWS News Blog
AWS News Blog
L
LINUX DO - 最新话题
P
Proofpoint News Feed
博客园 - 司徒正美
A
About on SuperTechFans
Latest news
Latest news
Scott Helme
Scott Helme
Hacker News: Ask HN
Hacker News: Ask HN
T
Threatpost
Hacker News - Newest:
Hacker News - Newest: "LLM"
C
CERT Recently Published Vulnerability Notes
Google DeepMind News
Google DeepMind News
博客园 - 聂微东

博客园 - aming

.Net线程 收集一些.NET开发资源站点和部分优 秀.NET开源项目 (转) 你必须知道的.net C#正则表达式整理备忘 ORACLE 基础 asp.net cache 深入认识javascript中的eval函数 FCKEditor的使用及配置方法 个人总结 javascript跨域问题 Javascript的IE和Firefox兼容问题 JavaScript的私有变量 typeof、instanceof、constructor与对象类型 javascript内存泄漏问题 JavaScript 中的内存泄露模式 jQuery Ajax 全解析 悟透JavaScript (转) JavaScript的9个陷阱及评点 全面理解javascript的caller,callee,call,apply概念(修改版)
浅析C# new和override的区别
aming · 2010-09-25 · via 博客园 - aming

C#中new和override是继承中经常用到的两个关键字,但是往往有时候容易把这两个关键字的作用搞混淆。

new

C# new关键字表示隐藏,是指加上new关键字的属性或函数将对本类和继承类隐藏基类的同名属性或函数

public class A
{
    
public virtual void Method()
    {
        Console.WriteLine(
"This Method in Class A!");
    }
}
public class B : A
{
    
public new void Method()
    {
        Console.WriteLine(
"This Method in Class B!");
    }
}

对于上面这个例子来说,假如运行A a=new B();a.Method();会输出This Method in Class A!,这是因为class B继承于class A,现在B中的Method函数隐藏A中的Method,所以从B(包括继承于B的子类)的角度来看类中的Method就是B.Method,A的Method不可见,但是如果从A的角度来看B,A只认识类B中继承于类A的Method函数,对于B类中的Method它不可见,所以A a=new B();a.Method();相当于是调用了类B中继承于A的Method函数

 

override

C#中override关键字表示重写,对于加上override关键字的属性或函数将完全覆盖基类的同名虚属性或虚函数,使基类的虚属性和虚函数在整个继承链中都不可见(在子类中用base关键字调用除外)。

public class A
{
    
public virtual void Method()
    {
        Console.WriteLine(
"This Method in Class A!");
    }
}
public class B : A
{
    
public override void Method()
    {
        Console.WriteLine(
"This Method in Class B!");
    }
}

对于上面这个例子来说,假如运行A a=new B();a.Method();会输出This Method in Class B!,因为class B的Method函数完全覆盖基类的同名虚函数Method,使整个继承链中看见的Method函数都是B中的Method,所以就算是以A角度来看B,A看到的Method函数也是B中的Method,因为A中的Method完全被B的覆盖了

但是如果要在B的对象中调用A的Method函数还是有办法,就是使用base关键字,比如

public class A
{
    
public virtual void Method()
    {
        Console.WriteLine(
"This Method in Class A!");
    }
}
public class B : A
{
    
public override void Method()
    {
        
base.Method();
    }
}

A a=new B();a.Method();会输出This Method in Class A!,base.Method();表示调用类B中继承于基类A的Method

new、override与interface

接口在相互继承的时候也会隐藏基接口的同名属性或函数,但是对于接口来说很特殊,隐藏对于基接口来说是不起作用的,接口内部的属性和函数都只是声明,它们都指向实现接口的类中的同名实现函数,通过接口调用接口的属性和函数的时候都会去调用实现类中从上到下最先可见的同名函数和同名属性:

public interface IA
{
    
void Method();
}
public interface IB : IA
{
    
new void Method();
}
public class IClass:IB
{
    
public void Method()
    {
        Console.WriteLine(
"This Method in Class IClass!");
    }
}
public class ISubClass : IClass
{
    
public new void Method()
    {
        Console.WriteLine(
"This Method in Class ISubClass!");
    }
}

IA ia = new ISubClass();ia.Method();输出This Method in Class IClass!因为对于ia来说在继承链中最先见到的同名实现函数是类IClass的Method函数

把上面的例子稍作修改:

public interface IA
{
    
void Method();
}
public interface IB : IA
{
    
new void Method();
}
public class IClass:IB
{
    
public virtual void Method()
    {
        Console.WriteLine(
"This Method in Class IClass!");
    }
}
public class ISubClass : IClass
{
    
public override void Method()
    {
        Console.WriteLine(
"This Method in Class ISubClass!");
    }
}

IA ia = new ISubClass();ia.Method();输出This Method in Class ISubClass!因为继承链中的Method函数都被ISubClass的Method覆盖了,所以对于ia来说在继承链中最先见到的同名实现函数是类ISubClass的Method函数