Override an overridden method (C#)

后端 未结 6 908
梦如初夏
梦如初夏 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: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

提交回复
热议问题