是否可以覆盖同一类(不是派生类)中的基类中的方法?

时间:2022-06-13 11:00:41

My understanding is that it is possible to Override a method (marked as Virtual) in a base class from a derived class using the keywords override.

我的理解是,可以使用关键字覆盖从派生类覆盖基类中的方法(标记为虚拟)。

But what about overriding a method in the same class?

但是在同一个类中重写一个方法呢?

In my specific case, I have a class x with 2 methods. Method a has some common behavior with b, and b adds functionality to method a.

在我的具体情况下,我有一个带有2种方法的类x。方法a与b有一些共同的行为,而b为方法a添加了功能。

In the past, I have duplicated the code of a in b and added the new functionality into b.

在过去,我复制了b中的a代码并将新功能添加到b中。

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

我想知道是否有更好的方法,所以我可以集中方法a的行为。

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

6 个解决方案

#1


11  

You could simply call your method a() from within your method b(). No need (and no way) to override a method.

您可以在方法b()中简单地调用方法a()。不需要(也没办法)覆盖方法。

public class x
{
   private static int _i = 0;

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

On the other hand you could have parameter overloads for your method.

另一方面,您可以为您的方法设置参数重载。

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
   }         
}

In this case you can call a(1) or a() with the same result.

在这种情况下,您可以使用相同的结果调用(1)或a()。

#2


9  

You can't override in the same class, you only do that in the derived class. You could put the common behavior of your method in method a and then call it from method b.

您不能在同一个类中覆盖,只能在派生类中执行此操作。您可以将方法的常见行为放在方法a中,然后从方法b中调用它。

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

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

You're free to call a from within b as many times as needed.

您可以根据需要多次从b内拨打电话。

#3


3  

For my understanding it is possible to Override a method (marked as Virtual) in a base class from a derived class using the keywords override. But what about the same override a method in the same class?

根据我的理解,可以使用关键字覆盖从派生类覆盖基类中的方法(标记为虚拟)。但是在同一个类中覆盖一个方法呢?

You cannot do this. Virtual methods are for Polymorphism, which is a fancy way of saying:

你不可以做这个。虚拟方法适用于多态,这是一种奇特的说法:

If you write a method that takes a base class, and pass it a derived class, and that method calls a method on the base class, it will actually call the derived implementation.

如果编写一个接受基类的方法,并将其传递给派生类,并且该方法在基类上调用方法,它实际上将调用派生实现。

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
    }
}

With that definition, it makes no sense to try to "override" a method within the same class.

根据该定义,尝试“覆盖”同一类中的方法是没有意义的。

In my specific case I have a Class x with 2 methods, method a as some common behavior with b, and b add functionality to method a.

在我的特定情况下,我有一个带有2个方法的类x,方法a作为b的一些常见行为,并且b为方法a添加功能。

Just call a() from b():

只需从b()调用():

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

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

#4


2  

Why don't you just call a() from b()?

你为什么不从b()调用()?

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

#5


1  

You split the common part into a third shared but private method.

您将公共部分拆分为第三个共享但私有的方法。

#6


1  

If I understand correctly your problem is that method b has the same code as method a plus some more code. Something like

如果我理解正确你的问题是方法b具有与方法相同的代码加上一些代码。就像是

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

    public void b()
    {
        // a code

        // b code
    }
}

if that is the case you could call method a in method b like this

如果是这种情况,你可以像这样在方法b中调用方法a

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

    public void b()
    {
        a();

        // b code
    }
}

The other case is that you have common code to both methods:

另一种情况是你有两个方法的共同代码:

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

        // a exclusive code
    }

    public void b()
    {
        // common code

        // b exclusive code
    }
}

Then you could extract that code to another method like this:

然后你可以将代码提取到另一个方法,如下所示:

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

        // a exclusive code
    }

    public void b()
    {
        CommonMethod();

        // b exclusive code
    }

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

And about overriding a method in the same class. It is called method overloading but it works allowing you to create several methods with the same name which differ from each other in terms of the type of the input.

关于覆盖同一类中的方法。它被称为方法重载,但它允许您创建多个具有相同名称的方法,这些方法在输入类型方面彼此不同。

Like this

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

    public string a(string s)
    {
    }
}

The only restriction is that the methods must have the same return type (string in this example).

唯一的限制是方法必须具有相同的返回类型(本例中为字符串)。

#1


11  

You could simply call your method a() from within your method b(). No need (and no way) to override a method.

您可以在方法b()中简单地调用方法a()。不需要(也没办法)覆盖方法。

public class x
{
   private static int _i = 0;

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

On the other hand you could have parameter overloads for your method.

另一方面,您可以为您的方法设置参数重载。

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
   }         
}

In this case you can call a(1) or a() with the same result.

在这种情况下,您可以使用相同的结果调用(1)或a()。

#2


9  

You can't override in the same class, you only do that in the derived class. You could put the common behavior of your method in method a and then call it from method b.

您不能在同一个类中覆盖,只能在派生类中执行此操作。您可以将方法的常见行为放在方法a中,然后从方法b中调用它。

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

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

You're free to call a from within b as many times as needed.

您可以根据需要多次从b内拨打电话。

#3


3  

For my understanding it is possible to Override a method (marked as Virtual) in a base class from a derived class using the keywords override. But what about the same override a method in the same class?

根据我的理解,可以使用关键字覆盖从派生类覆盖基类中的方法(标记为虚拟)。但是在同一个类中覆盖一个方法呢?

You cannot do this. Virtual methods are for Polymorphism, which is a fancy way of saying:

你不可以做这个。虚拟方法适用于多态,这是一种奇特的说法:

If you write a method that takes a base class, and pass it a derived class, and that method calls a method on the base class, it will actually call the derived implementation.

如果编写一个接受基类的方法,并将其传递给派生类,并且该方法在基类上调用方法,它实际上将调用派生实现。

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
    }
}

With that definition, it makes no sense to try to "override" a method within the same class.

根据该定义,尝试“覆盖”同一类中的方法是没有意义的。

In my specific case I have a Class x with 2 methods, method a as some common behavior with b, and b add functionality to method a.

在我的特定情况下,我有一个带有2个方法的类x,方法a作为b的一些常见行为,并且b为方法a添加功能。

Just call a() from b():

只需从b()调用():

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

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

#4


2  

Why don't you just call a() from b()?

你为什么不从b()调用()?

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

#5


1  

You split the common part into a third shared but private method.

您将公共部分拆分为第三个共享但私有的方法。

#6


1  

If I understand correctly your problem is that method b has the same code as method a plus some more code. Something like

如果我理解正确你的问题是方法b具有与方法相同的代码加上一些代码。就像是

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

    public void b()
    {
        // a code

        // b code
    }
}

if that is the case you could call method a in method b like this

如果是这种情况,你可以像这样在方法b中调用方法a

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

    public void b()
    {
        a();

        // b code
    }
}

The other case is that you have common code to both methods:

另一种情况是你有两个方法的共同代码:

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

        // a exclusive code
    }

    public void b()
    {
        // common code

        // b exclusive code
    }
}

Then you could extract that code to another method like this:

然后你可以将代码提取到另一个方法,如下所示:

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

        // a exclusive code
    }

    public void b()
    {
        CommonMethod();

        // b exclusive code
    }

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

And about overriding a method in the same class. It is called method overloading but it works allowing you to create several methods with the same name which differ from each other in terms of the type of the input.

关于覆盖同一类中的方法。它被称为方法重载,但它允许您创建多个具有相同名称的方法,这些方法在输入类型方面彼此不同。

Like this

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

    public string a(string s)
    {
    }
}

The only restriction is that the methods must have the same return type (string in this example).

唯一的限制是方法必须具有相同的返回类型(本例中为字符串)。