简体   繁体   English

C#中方法隐藏和方法重写的结合

[英]Combining Method hiding and method overriding in C#

I just could not find a satisfactory explanation for this. 我只是找不到令人满意的解释。 So I thought it would help to post this at SO. 因此,我认为将其发布在SO上将有所帮助。

What happens when we combine method hiding and overriding in C# ? 当我们method hiding and overriding in C# ?结合使用method hiding and overriding in C# ?时会发生什么method hiding and overriding in C# ?

For the below example : 对于以下示例:

class BaseClassA
{
    public virtual void showMessage()
    {
        Console.WriteLine("In BaseClass A ");
    }
}
class DerivedClassB : BaseClassA
{
    public override void showMessage()
    {
        Console.WriteLine("In DerivedClass B ");
    }
}

class DerivedClassC : DerivedClassB
{
    public new void showMessage()
    {
        Console.WriteLine("In DerivedClass C");
    }
}

class Program
{
    static void Main(string[] args)
    {
        BaseClassA a = new BaseClassA();
        a.showMessage();
        a = new DerivedClassB();
        a.showMessage();
        BaseClassA b = new DerivedClassC();
        b.showMessage();
        Console.ReadKey();   
    }
}

I am understanding the output of 我了解的输出

 BaseClassA b = new DerivedClassC();
 b.showMessage();

在此处输入图片说明

Here's what I understand for new and override in C# 这是我对C#中的newoverride内容的理解

New - It hides the baseclass method. New -隐藏基类方法。 So even if baseclass reference variable points to a derived class object if that derived class hides the method, the output will be baseclass output only. 因此,即使基类引用变量指向派生类对象(如果该派生类隐藏了该方法),其输出也仅是基类输出。

Override - It overrides the baseclass method. Override -覆盖基类方法。 So even if baseclass reference variable points to a derived class object if that derived class overrides the method, the output will be derived class output. 因此,即使该基类引用变量指向该派生类对象(如果该派生类覆盖该方法),该输出也将是派生类输出。

But here how can even a BaseClassA reference variable point to a DerivedClassC object and print DerivedClassB 's output ? 但是在这里,哪怕一个BaseClassA引用变量如何指向DerivedClassC对象并打印DerivedClassB的输出呢?

Please explain in simple words. 请用简单的话解释。

But here how can even a BaseClassA reference variable point to DerivedClassC object and it prints DerivedClassB's output ? 但是在这里,哪怕一个BaseClassA引用变量如何指向DerivedClassC对象并输出DerivedClassB的输出呢?

The code calls the method which is declared by BaseClassA but overridden by DerivedClassB . 该代码调用由BaseClassA声明但被DerivedClassB覆盖的DerivedClassB The method declared in DerivedClassC is a new method, entirely separate from the one declared in BaseClassA ... as if it had a different name, in a way. DerivedClassC声明的方法是一种新方法,与在BaseClassA声明的方法完全不同...好像在某种程度上它具有不同的名称。

Effectively, think of it this way: 实际上,可以这样考虑:

  • If DerivedClassC didn't declare a showMessage method at all, it would inherit the implementation from DerivedClassB , right? 如果DerivedClassC根本不声明showMessage方法,它将继承DerivedClassB的实现,对吗?
  • Introducing a new method doesn't change the output because it's a separate method which isn't involved in overriding the method introduced in DerivedClassA . 引入方法不会更改输出,因为它是一个单独的方法,不会覆盖重写DerivedClassA引入的方法。 So the output is the same as with the previous step. 因此,输出与上一步相同。

I think that trying DerivedClassC as just 我认为尝试DerivedClassC只是

class DerivedClassC : DerivedClassB
{
}

and understanding that output is the key to understanding the later behaviour. 和理解输出的关键是理解后的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM