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

推荐订阅源

Know Your Adversary
Know Your Adversary
阮一峰的网络日志
阮一峰的网络日志
V
Visual Studio Blog
H
Help Net Security
博客园 - Franky
博客园_首页
博客园 - 【当耐特】
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
腾讯CDC
人人都是产品经理
人人都是产品经理
T
Tailwind CSS Blog
博客园 - 三生石上(FineUI控件)
爱范儿
爱范儿
博客园 - 聂微东
小众软件
小众软件
宝玉的分享
宝玉的分享
美团技术团队
WordPress大学
WordPress大学
L
LINUX DO - 热门话题
S
Secure Thoughts
IT之家
IT之家
博客园 - 叶小钗
Apple Machine Learning Research
Apple Machine Learning Research
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
有赞技术团队
有赞技术团队
V
V2EX
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The Cloudflare Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
量子位
G
GRAHAM CLULEY
Attack and Defense Labs
Attack and Defense Labs
Jina AI
Jina AI
罗磊的独立博客
Security Archives - TechRepublic
Security Archives - TechRepublic
W
WeLiveSecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Tenable Blog
L
Lohrmann on Cybersecurity
S
SegmentFault 最新的问题
J
Java Code Geeks
Last Week in AI
Last Week in AI
Cyberwarzone
Cyberwarzone
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
大猫的无限游戏
大猫的无限游戏
T
Tor Project blog
酷 壳 – CoolShell
酷 壳 – CoolShell
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
H
Heimdal Security Blog

博客园 - 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函数