简体   繁体   中英

Calling method of non-immediate relative class

Is it possible to call the base of a base method? IE a not an 'immediate' ancestor of a derived class?

public class a
{
    public virtual void test() { }
}

public class b : a
{
    public override void test()
    {
        //...
        base.test();
    }
}

public class c : b
{
    public override void test()
    {
        //...
        //possible to call test() in  class a, without calling it in class b? 
        //((a)base).test(); //doesnt work

    }
}

No, that's not possible and goes against OOP principles. If you need to do that you probably designed your system wrong.

Not with C#. C# only allows you to call methods on the immediate base class.

The CLR on the other hand allows it. You can do it with reflection, or by writing IL directly. I assume C++/CLI supports this too.

But I'd reconsider the design. Skipping an override smells of bad design and breaks the encapsulation of your base class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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