从基础强制方法调用子类的新方法 [英] Force method from base to call a subclass new method

查看:50
本文介绍了从基础强制方法调用子类的新方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的理解是,子类的行为与父类完全相同,除了其他方法或使用 new override 关键字重写"的方法.显然,这是不正确的.

My understanding is that a subclass behaves exactly like the parent class except for the additional methods or those that get "rewritten" with the new and override keywords. Apparently, that's not correct.

请参见下面的代码(为了便于阅读,删除了括号)

Please see the code below (brackets removed for readability)

class BIG
    protected void Output(string text = "")
        Console.WriteLine("BIG - " + text);

    public void CallingOutput()
        Output("Outputting...");

class SMALL:BIG
    public new void Output(string text = "")
        Console.WriteLine("SMALL - " + text);

class Program
    static void Main(string[] args)
        SMALL foo = new SMALL();

我已经知道我可以使用 new 关键字将 BIG.Output(string)隐藏在SMALL中.直接拨打电话时,效果很好.

I already know that I can hide the BIG.Output(string) in SMALL with the new keyword. When calling directly, it works perfectly.

>> foo.Output("Outputting...")
SMALL - Outputting...

在这里与我的理解相抵触.我认为定义 SMALL:BIG 类与定义 SMALL 类并从BIG复制粘贴所有方法完全相同.简而言之,我认为上面的SMALL类就是这样的:

Here is where it conflicts with my understanding. I thought defining class SMALL:BIG was exactly the same as if I define class SMALL and copy-paste all method from BIG. In short, I thought the SMALL class above was equivalent to this:

class SMALL
    public void Output(string text = "")
        Console.WriteLine("SMALL - " + text);

    public void CallingOutput()
        Output("Outputting...");

显然这是不正确的,因为

Apparently that is not correct because

>> foo.CallingOutput()
BIG - Outputting...

当间接调用时,它仍然使用 BIG 中原始的 Output(string).

It is still using the original Output(string) from BIG when called indirectly.

正确的方法是什么使 foo.CallingOutput()输出

What is the right way of doing it so that foo.CallingOutput() would output

小-正在输出..."

假设我无权访问 BIG ,并且不允许进行任何更改.而且我也不想隐藏 CallingOutput(),因为这基本上是在重写类.(是的,隐藏 CallingOutput()使其起作用)

Assuming that I don't have access to BIG and not allowed to make any changes. And I don't want to have to hide CallingOutput() also because that is basically rewriting the class. (Yes, hiding CallingOutput() makes it work)

我在发布之前进行了一些搜索,并在 C ++ 中发现了类似的问题.但是,不可能做到这一点.

I did some searching before posting this and found a similar question in C++. However, the answer for that is not possible.

  1. 我不相信.如此强大的语言,却无法告诉编译器哪种方法.
  2. 这是另一种语言,因此该帖子被允许,因为它不能被视为重复.

推荐答案

创建填充片

您不能修改或覆盖现有类中的任何内容,除非它被设计为被覆盖.这是设计使然.

Create a shim

You can't modify or override anything in the existing class unless it was designed to be overridden. This is by design.

话虽如此,您可以创建一个新的"shim"(直通)类,该类与原始类完全相同,但成员被声明为虚拟.这可以通过同时使用 new virtual 修饰符来实现.

That being said, you can create a new "shim" (pass-through) class that is exactly like the original class but with the member being declared as virtual. This is accomplished by using both new and virtual modifiers.

一旦有了shim类,就可以按常规覆盖该方法.示例:

Once you have the shim class, you can override the method per normal. Example:

class Big
{
    public void Output(string text) { Console.WriteLine("Big - {0}", text); }

    public void CallingOutput()
    {
        Output("Outputting...");
    }
}

class Shim : Big
{
    public new virtual void Output(string text) { base.Output(text); }

    public void CallingOutput()
    {
        Output("Outputting...");
    }
}

现在,您有了Shim类,该类将允许您想要的内容.因此,将其子化(而不是Big),然后看看会发生什么:

Now you have a Shim class that will allow what you want. So subclass it (instead of Big) and see what happens:

class Small : Shim
{
    public override void Output(string text) { Console.WriteLine("Small - {0}", text); }
}

测试:

Shim s = new Shim();
s.CallingOutput();   //Original behavior

Shim s = new Small();
s.CallingOutput();   //Overridden behavior

输出:

Big - Outputting...
Small - Outputting...

DotNetFiddle上的示例

这篇关于从基础强制方法调用子类的新方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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