C# new与override的介绍与区别

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!");
    }
}

意思也就是 子类使用new 重写方法后  父类 A=new  子类();  则站在A的角度调用到的是 A.Method()  而调用不到B.Method(),而当 子类 B=new  子类(); 则调用B.Method(),而调用不到父类A的Method()。(当然 new 和override的子类中都可以用base 调用父类的Method())。 总结通俗的讲:new:父类看不到子类的new的新方法,子类看不到父类被new的方法, 而override: 父类和子类看到的都是子类override后的方法。

对于上面这个例子来说,假如运行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。

newoverrideinterface

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

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函数。

总结

其实挺好理解的!

原文链接:https://www.cnblogs.com/tiancai/p/4591731.html

感谢!

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值