简体   繁体   English

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

[英]Is it possible to override a Method in a Base Class within the same Class (NOT a Derived Class)?

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 .我的理解是,可以使用关键字override从派生的 class 中覆盖基础 class 中的方法(标记为Virtual )。

But what about overriding a method in the same class?但是在同一个 class 中覆盖一个方法呢?

In my specific case, I have a class x with 2 methods.在我的具体情况下,我有一个 class x有 2 种方法。 Method a has some common behavior with b , and b adds functionality to method a .方法ab有一些共同的行为,并且b为方法a添加了功能。

In the past, I have duplicated the code of a in b and added the new functionality into b .过去,我将a的代码复制到b中,并将新功能添加到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() {}         
    }

You could simply call your method a() from within your method b() .您可以简单地从您的方法b() a() No need (and no way) to override a method.不需要(也没有办法)覆盖方法。

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.在这种情况下,您可以使用相同的结果调用a(1)a()

You can't override in the same class, you only do that in the derived class.您不能在同一个 class 中覆盖,您只能在派生的 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 中调用 a。

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.据我了解,可以使用关键字 override 从派生的 class 中覆盖基础 class 中的方法(标记为虚拟)。 But what about the same override a method in the same class?但是,在同一个 class 中,同样的覆盖方法又如何呢?

You cannot do this.你不能做这个。 Virtual methods are for Polymorphism , which is a fancy way of saying:虚拟方法用于Polymorphism ,这是一种奇特的说法:

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 的方法,并将派生的 class 传递给它,并且该方法调用基 class 上的方法,它实际上将调用派生的实现。

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.使用该定义,尝试“覆盖”同一 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.在我的具体情况下,我有一个 Class x 有 2 个方法,方法 a 作为 b 的一些常见行为,b 向方法 a 添加功能。

Just call a() from b() :只需从b()调用a() () :

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

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

Why don't you just call a() from b() ?为什么不直接从b()调用a() () ?

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

You split the common part into a third shared but private method.您将公共部分拆分为第三个共享但私有的方法。

If I understand correctly your problem is that method b has the same code as method a plus some more code.如果我理解正确,您的问题是方法 b 具有与方法 a 相同的代码加上更多代码。 Something like就像是

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.关于在同一个 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).唯一的限制是方法必须具有相同的返回类型(本例中为字符串)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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