覆盖被覆盖的方法 (C#)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1152925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 09:29:05  来源:igfitidea点击:

Override an overridden method (C#)

c#inheritance

提问by Robert W

I'm trying to override an overridden method (if that makes sense!) in C#.

我试图在 C# 中覆盖一个被覆盖的方法(如果这有意义的话!)。

I have a scenario similar to the below, but when I have a breakpoint in the SampleMethod() in the "C" class it's not being hit, whilst the same breakpoint in the "B" method is being hit.

我有一个类似于下面的场景,但是当我在“C”类的 SampleMethod() 中有一个断点时,它没有被命中,而“B”方法中的相同断点被命中。

public class A
{
      protected virtual void SampleMethod() {}
}

public class B : A 
{
      protected override void SampleMethod()
      {
           base.SampleMethod(); 
      }
}

public class C : B
{
      protected override void SampleMethod() 
      {
           base.SampleMethod(); 
      }
}

Thanks in advance!

提前致谢!



Edit:

编辑:

Ok, the context would help:

好的,上下文会有所帮助:

This is in the context of a composite control so class A inherits from CompositeControl and calls SampleMethod() after overriding the CreateChildControls() method.

这是在复合控件的上下文中,因此类 A 继承自 CompositeControl 并在覆盖 CreateChildControls() 方法后调用 SampleMethod()。

采纳答案by Colin Mackay

Without seeing the code that calls SampleMethod, my guess would be that you have an object of type B and call SampleMethod on that.

如果没有看到调用 SampleMethod 的代码,我的猜测是您有一个 B 类型的对象并在其上调用 SampleMethod 。

回答by Noldorin

Overriding can be performed in a chain as long as you like. The code you have shown is correct.

只要您愿意,可以在链中执行覆盖。您显示的代码是正确的。

The only possible explanation for the behaviour you are seeing is that the object to which you are referring is actually of type B. I suggest that you double check this, and if things still don't make sense, post the other appropiate code.

您所看到的行为的唯一可能解释是您所指的对象实际上是 type B。我建议您仔细检查一下,如果仍然没有意义,请发布其他适当的代码。

回答by Callum Rogers

That solution works fine; although to actually use it outside the class the method is in, you need to set the access of SampleMethodto publicrather than protectedin all of the cases it appears, so:

该解决方案工作正常;尽管要在方法所在的类之外实际使用它,您需要设置SampleMethodtopublic而不是protected在它出现的所有情况下的访问,因此:

public class A
{
    public virtual void SampleMethod() 
    {
        Console.WriteLine("lol");
    }
}

public class B : A
{
    public override void SampleMethod()
    {
        base.SampleMethod();
    }
}

public class C : B
{
    public override void SampleMethod()
    {
        base.SampleMethod();
    }
}

回答by Mike J

The breakpoint is more than likely not being hit because you actually instantiated an instance of the "B" class.

断点很可能没有被击中,因为您实际上实例化了“B”类的一个实例。

Method override resolution works based on the actual runtimetype of the class whose method should be called. So, if you had the following code:

方法覆盖解析基于应调用其方法的类的实际运行时类型。所以,如果你有以下代码:

C c = new C();
c.SampleMethod();

and the following:

以及以下内容:

C c = new C();
B b = (B)c;
b.SampleMethod();

both the runtimetypes of the class whose SampleMethodwill be called is type B.

将被调用的类的两个运行时类型SampleMethod都是 type B

回答by Ahmad Salam Mehsud

Method overriding is OOP feature that allows a child class to provide a specific implementation of a method that is already provided by one of its parent classes.

方法覆盖是 OOP 功能,它允许子类提供已由其父类之一提供的方法的特定实现。

回答by altamimi.morad

for Overriding more than once in the hierarchy use something like this

为了在层次结构中多次覆盖使用这样的东西

// abstract class
abstract class A
    {
        public abstract void MethodOne();
    }

// class B inherits A
class B : A
{
    public override void MethodOne()
    {
        Console.WriteLine("From Class B");
    }
}

// class C inherits B
class C : B
{
    public override void MethodOne()
    {
        Console.WriteLine("From Class C");
    }
}

// class D inherits C
class D : C
{
    public override void MethodOne()
    {
        Console.WriteLine("From Class D");
    }
}

// etc......

// class Main method Class

class MainClass
{
    public static void Main()
    {
        B[] TestArray = new B[3];
        B b1 = new B();
        C c1 = new C();
        D d1 = new D();

        TestArray[0] = b1;
        TestArray[1] = c1;
        TestArray[2] = d1;

        for (int i = 0; i < TestArray.Length; i++)
        {
            TestArray[i].MethodOne();
        }

        Console.ReadLine();
    }
}

I did it in this code in this link http://www.4shared.com/rar/0SG0Rklxce/OverridingMoeThanOnce.html

我在这个链接中的这段代码中做到了 http://www.4shared.com/rar/0SG0Rklxce/OverridingMoeThanOnce.html