C# 从父类调用子类方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9067885/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 05:46:54  来源:igfitidea点击:

Calling child class method from parent

c#oopinheritancepolymorphismoverriding

提问by user1178494

Is it possible for the a.doStuff() method to print "B did stuff" without editing the A class? If so, how would I do that?

a.doStuff() 方法是否可以在不编辑 A 类的情况下打印“B did stuff”?如果是这样,我该怎么做?

class Program
{
    static void Main(string[] args)
    {
        A a = new A();
        B b = new B();

        a.doStuff();
        b.doStuff();

        Console.ReadLine();
    }
}

class A
{
    public void doStuff()
    {
        Console.WriteLine("A did stuff");
    }
}

class B : A
{
    public void doStuff()
    {
        Console.WriteLine("B did stuff");
    }
}

I'm modding a steam game, Terraria. And I don't want to decompile and recompile it all because that will screw with steam. My program 'injects' into Terraria via XNA. I can use the update() and draw() methods from XNA to mod some things. But it's pretty limited. I wan't to override base methods to mod more things (worldgen for example).

我正在修改 Steam 游戏,泰拉瑞亚。而且我不想反编译并重新编译它,因为这会影响蒸汽。我的程序通过 XNA“注入”到泰拉瑞亚。我可以使用 XNA 的 update() 和 draw() 方法来修改一些东西。但它非常有限。我不想覆盖基本方法来修改更多东西(例如 worldgen)。

回答by Aliostad

Yes, if you declare doStuffas virtualin Aand then overridein B.

是的,如果你声明doStuffvirtualinA然后overridein B

class A
{
    public virtual void doStuff()
    {
        Console.WriteLine("A did stuff");
    }
}

class B : A
{
    public override void doStuff()
    {
        Console.WriteLine("B did stuff");
    }
}

回答by mdprotacio

Since B is effectively A through inheritance and the method is overloaded.

由于 B 通过继承实际上是 A 并且该方法被重载。

A a = new B();
a.doStuff();

回答by VS1

The code for class A & B you have posted will anyways generate below compiler warning and will ask to use the newkeyword on class B, although it will compile: The keyword new is required on 'B.doStuff()' because it hides inherited member 'A.doStuff()'

您发布的 A 类和 B 类代码无论如何都会生成以下编译器警告,并会要求new在类 B 上使用关键字,尽管它会编译: 'B.doStuff()' 上需要关键字 new,因为它隐藏了继承的成员'A.doStuff()'

Use method hidingalong with newand virtualkeyword in class Mapper and class B as follows:

使用method hiding沿newvirtual类映射器和B类的关键字如下:

class Program
{
    static void Main(string[] args)
    {
        Mapper a = new B(); //notice this line
        B b = new B();

        a.doStuff();
        b.doStuff();

        Console.ReadLine();
    }
}

class A
{
    public void doStuff()
    {
        Console.WriteLine("A did stuff");
    }
}

class Mapper : A
{
    new public virtual void doStuff() //notice the new and virtual keywords here which will all to hide or override the base class implementation
    {
        Console.WriteLine("Mapper did stuff");
    }
}

class B : Mapper
{
    public override void doStuff()
    {
        Console.WriteLine("B did stuff");
    }
}