C # - Is it possible to override a method in a base class within the same class (NOT a derived class)?

I understand that you can override a method (marked as Virtual) in a base class from a derived class using keywords override.

But what about overriding a method in one class?

In my specific case, I have a class xwith two methods. The method ahas some common behavior with b, but badds functions to the method a.

In the past, I duplicated code ain band added new functionality to b.

I would like to know if there is a better approach, so I can centralize the behavior of the method a.

    public class x
    {
       public static void a() {}
       public static void b() {}         
    }

Thank you for your time: -)!

+5
6

a() b(). ( ) .

public class x
{
   private static int _i = 0;

   public static void a() {
       _i = 1;
   }
   public static void b() {
       a();
       // here is _i = 1;
   }         
}

, .

public class x
{
   private static int _i = 0;

   public static void a() {
       a(1);
   }
   public static void a(int i) {
      _i = t;
      // common code which can use _i
   }         
}

a(1) a() .

+9

, . a, b.

public static void a() 
{
// some common functionality
}

public static void b()
{
// do something
a(); //call a
//do something else
}

a b , .

+8

( ) . ?

. Polymorphism, :

, , , .

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

class B : A
{
    public override void DoSomething() { Console.WriteLine("From B"); }
}

class C
{
    public void DoSomethingMagic(A someClassInstance)
    {
        someClassInstance.DoSomething();
    }
}

class Program
{
    public static void Main(string[] args)
    {
        A a = new A();
        B b = new B();
        C c = new C();
        c.DoSomethingMagic(a); // Prints "From A"
        c.DoSomethingMagic(b); // Prints "From B", even though it takes an A

        A bIsAnA = new B();
        c.DoSomethingMagic(bIsAnA); // Prints "From B", because B is also an A
    }
}

"" .

x 2 , a b b a.

a() b():

public class x
{
   public static void a()
   {
       // "base" implementation here
   }

   public static void b()
   {
       a();
       // extra implementation here
   }
}
+3

a() b()?

public static void b()
{
    // Do some stuff
    a(); // Do the common stuff
    // Do more stuff
}
+2

, .

+1

, , b , a . -

public class x
{
    public void a()
    {
        // a code
    }

    public void b()
    {
        // a code

        // b code
    }
}

, a b,

class x
{
    public void a()
    {
        // a code
    }

    public void b()
    {
        a();

        // b code
    }
}

:

class x
{
    public void a()
    {
        // common code

        // a exclusive code
    }

    public void b()
    {
        // common code

        // b exclusive code
    }
}

, :

class x
{
    public void a()
    {
        CommonMethod();

        // a exclusive code
    }

    public void b()
    {
        CommonMethod();

        // b exclusive code
    }

    private void CommonMethod()
    {
        // common code
    }
}

. , , , .

class x
{
    public string a(int i)
    {
    }

    public string a(string s)
    {
    }
}

, ( ).

+1

All Articles