新vs覆盖关键字 [英] new vs override keywords

查看:85
本文介绍了新vs覆盖关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于多态方法的问题.我有两个类:具有非虚拟方法Foo( )的基类,它调用其虚拟方法Foo (int i)(例如:Foo() {Foo(1);}))和派生类,其覆盖方法Foo(int i).

I've got a question concerning polymorphic methods. I've got two classes: the base class with the non-virtual method Foo( ) which calls its virtual method Foo (int i) (like this: Foo() {Foo(1);}) and the derived class which overrides method Foo(int i).

如果我调用派生类实例的Foo()方法,则演练如下:base Foo() -> override Foo(int i).但是,如果我将覆盖方法更改为新方法,则演练如下:base Foo -> base Foo(int i).它甚至没有涉及到新的Foo(int i)方法.请说明这些方法的顺序及其原因.

If I call Foo() method of an instance of the derived class the walkthrough is as the following: base Foo() -> override Foo(int i). But if I change override method to new the walkthrough is as the following: base Foo -> base Foo(int i). It doesn't even get to the new Foo(int i) method. Please, explain the sequence of those methods and why it is the way it is.

using System;
class Program
{
    sealed void Main()
    {
        DerivedClass d = new DerivedClass();
        //Goes to BaseClass Foo() method
        //then goes to Derived Foo(int i ) method
        d.Foo();
    }
}
class BaseClass
{
    public void Foo() { Foo(1); }
    public virtual void Foo(int i) { // do something;
    }
}
class DerivedClass : BaseClass
{
    public override void Foo(int i)  { //Do something
    }
}

//////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////

using System;
    class Program
    {
        sealed void Main()
        {
            DerivedClass d = new DerivedClass();
            //Goes to BaseClass Foo() method
            //then goes to base Foo(int i) method
            //never gets to Foo(int i)  of the derived class
            d.Foo();
        }
    }
    class BaseClass
    {
        public void Foo() { Foo(1); }
        public virtual void Foo(int i) { // do something;
        }
    }
    class DerivedClass : BaseClass
    {
        public new void Foo(int i)  { //Do something
        }
    }

推荐答案

(使用new时.)

它甚至都没有使用新的Foo(int i)方法.

It doesn't even get to the new Foo(int i) method.

是的-但是它执行Foo(int)BaseClass实现,因为在派生类中它没有被 overrided 覆盖.这就是new的全部要点-就是说,我没有重写基类方法-我是一个全新的方法."如果要覆盖基类方法,请使用override.线索在关键字中:)

Yes it does - but it executes the BaseClass implementation of Foo(int) because it's not overridden in the derived class. That's the whole point of new - it's saying, "I'm not overriding a base class method - I'm a whole new method." If you want to override the base class method, use override. The clue is in the keyword :)

例如,当使用new时:

BaseClass x = new DerivedClass();
x.Foo(1); // Calls BaseClass.Foo(int)

DerivedClass y = new DerivedClass();
y.Foo(1); // Calls DerivedClass.Foo(int)

但是使用override时:

BaseClass x = new DerivedClass();
x.Foo(1); // Calls DerivedClass.Foo(int) // Due to overriding

DerivedClass y = new DerivedClass();
y.Foo(1); // Calls DerivedClass.Foo(int)

这篇关于新vs覆盖关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆