如何让基类调用它的虚函数?

sga*_*arg 1 c#

例如在下面的程序我想entrypoint()A能够打电话 doThisA,但它调用doThisB中,因为这是实例类型。我怎样才能达到我想要的?

void Main()
{
    A a = new B();
    a.entrypoint();
}

public class A
{
    public void entrypoint()
    {
        this.doThis(); //doesn't call A::doThis()
    }

    public virtual void doThis()
    {
        Console.WriteLine ("From A");
    }
}

public class B : A
{
    public override void doThis()
    {
        Console.WriteLine ("From B");
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑:如何调用重写的虚拟方法的“基本实现”?是相似的,但我是从基类而不是主类调用基实现。

编辑:类似于即使当前实例是派生类,我们如何从基类中的另一个方法调用虚拟方法?

its*_*e86 5

如果您需要它不被覆盖,正确的方法可以说是不调用虚拟方法。我建议创建一个私有方法来做你想做的事。