Override an overridden method (C#)

后端 未结 6 890
梦如初夏
梦如初夏 2021-02-03 17:47

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

I have a scenario similar to the below, but when I have a breakpoint in the SampleMethod() in

相关标签:
6条回答
  • 2021-02-03 18:38

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

    Method override resolution works based on the actual runtime type 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 runtime types of the class whose SampleMethod will be called is type B.

    0 讨论(0)
  • 2021-02-03 18:39

    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.

    0 讨论(0)
  • 2021-02-03 18:44

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

    0 讨论(0)
  • 2021-02-03 18:48

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

    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();
        }
    }
    
    0 讨论(0)
  • 2021-02-03 18:53

    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

    0 讨论(0)
  • 2021-02-03 18:54

    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.

    0 讨论(0)
提交回复
热议问题