How to skip the base class override function

I have the following class structure and there are many classes, such as C, derived from B, in some classes that I do not want to use B.OnShow (), but I want A.OnShow () to be executed with C Any tricks?

abstract class A
{
   protected virtual void OnShow()
   {
        //some code
        base.OnShow();
   }
}

class B : A
{
    protected override void OnShow()
    {
      //some other code
      base.OnShow();
    }
}

class C : B
{
   protected override void OnShow()
   {
      //some other code
      base.OnShow();
   }
}
+3
source share
3 answers

You do not need C to expand from B, instead create a new class “X” that extends from A and has the parts that are currently in B that you want for B and C, and then C and B extend from " X". Then B will just have the bits in it that you want for B specifically (a bit abstract, but I think it makes sense).

, , - .

+5

, #.

+2

. C B, , , B OnShow. , , - , , , .

, :

abstract class A
{
    protected virtual void OnShow()
    {
        UniqueOnShowStuff();
        base.OnShow();
    }
    protected virtual void UniqueOnShowStuff()
    {
     //
    }
}

class B : A
{
    protected override void OnShow()
    {
        // Things all B need to Show
        base.OnShow();
    }
    protected override void UniqueOnShowStuff()
    {
        // Things most B might want to show but don't have to
    }
}

class C : B
{
    protected override void OnShow()
    {
        // Things all C need to show
        base.OnShow();
    }
    protected override void UniqueOnShowStuff()
    {
        // override without a base call so you don't show B things
    }
}

, , , / .

+2

Source: https://habr.com/ru/post/1734628/


All Articles