来来来,测试一下C#的override和new

  • override是指“覆盖”,是指子类覆盖了父类的方法。子类的对象无法再访问父类中的该方法。
  • new是指“隐藏”,是指子类隐藏了父类的方法,通过一定的转换,可以在子类的对象中访问父类的方法。

 

先弄个鸡肋

像这样吧


    class BaseClass
    {
        public virtual void SendMsg1()
        {
            var method = MethodBase.GetCurrentMethod();
            var className = method.DeclaringType.Name;
            Console.WriteLine("露馅了,我是鸡肋方法:"+className + "." + method.Name);
        }

        public virtual void SendMsg2()
        {
            var method = MethodBase.GetCurrentMethod();
            var className = method.DeclaringType.Name;
            Console.WriteLine("露馅了,我是鸡肋方法:" + className + "." + method.Name);
        }

        public virtual void SendMsg3()
        {
            var method = MethodBase.GetCurrentMethod();
            var className = method.DeclaringType.Name;
            Console.WriteLine("露馅了,我是鸡肋方法:" + className + "." + method.Name);
        }

        public void SendMsg4()
        {
            var method = MethodBase.GetCurrentMethod();
            var className = method.DeclaringType.Name;
            Console.WriteLine("露馅了,我是鸡肋方法:" + className + "." + method.Name);
        }

        public void SendMsg5()
        {
            var method = MethodBase.GetCurrentMethod();
            var className = method.DeclaringType.Name;
            Console.WriteLine("露馅了,我是鸡肋方法:" + className + "." + method.Name);
        }

        public virtual void SendMsg6()
        {
            var method = MethodBase.GetCurrentMethod();
            var className = method.DeclaringType.Name;
            Console.WriteLine("露馅了,我是鸡肋方法:" + className + "." + method.Name);
        }

        public void SendMsg7()
        {
            var method = MethodBase.GetCurrentMethod();
            var className = method.DeclaringType.Name;
            Console.WriteLine("露馅了,我是鸡肋方法:" + className + "." + method.Name);
        }
    }

 

 

在整个紫类

 


    class SubClass1:BaseClass
    {
        public override void SendMsg1()
        {
            var method = MethodBase.GetCurrentMethod();
            var className = method.DeclaringType.Name;
            Console.WriteLine("我是紫类,我要遮天:" + className + "." + method.Name);
        }

        public new void SendMsg2()
        {
            var method = MethodBase.GetCurrentMethod();
            var className = method.DeclaringType.Name;
            Console.WriteLine("我是紫类,我要遮天:" + className + "." + method.Name);
        }

        public void SendMsg3()
        {
            var method = MethodBase.GetCurrentMethod();
            var className = method.DeclaringType.Name;
            Console.WriteLine("我是紫类,我要遮天:" + className + "." + method.Name);
        }

        public void SendMsg4()
        {
            var method = MethodBase.GetCurrentMethod();
            var className = method.DeclaringType.Name;
            Console.WriteLine("我是紫类,我要遮天:" + className + "." + method.Name);
        }

        public new void SendMsg5()
        {
            var method = MethodBase.GetCurrentMethod();
            var className = method.DeclaringType.Name;
            Console.WriteLine("我是紫类,我要遮天:" + className + "." + method.Name);
        }
    }

 

喏,就这样

 

然后主函数中,这样,随便一下

 


        private static void Main(string[] args)
        {
            TestVirtual1();
            Console.ReadLine();
        }

        public static void TestVirtual1()
        {
            BaseClass bc=new SubClass1();
            bc.SendMsg1();
            bc.SendMsg2();
            bc.SendMsg3();
            bc.SendMsg4();
            bc.SendMsg5();
            bc.SendMsg6();
            bc.SendMsg7();
            Console.WriteLine();
            SubClass1 bc2 = new SubClass1();
            bc2.SendMsg1();
            bc2.SendMsg2();
            bc2.SendMsg3();
            bc2.SendMsg4();
            bc2.SendMsg5();
            bc2.SendMsg6();
            bc2.SendMsg7();
            Console.WriteLine();
            BaseClass bc3 = new SubClass1();
            ((BaseClass) bc2).SendMsg1();
            ((BaseClass)bc2).SendMsg2();
            ((BaseClass)bc2).SendMsg3();
            ((BaseClass)bc2).SendMsg4();
            ((BaseClass)bc2).SendMsg5();
            ((BaseClass)bc2).SendMsg6();
            ((BaseClass)bc2).SendMsg7();
        }

 

然后F5,啵啵啵啵

 

来来来,测试一下C#的override和new_第1张图片

 

 

可以看到,鸡肋中设置了7块肋骨(方法),注意有虚方法

然后,紫类,只有SendMsg1使用override重写,其余用了new或无修饰

 

第一组,使用BaseClass bc=new SubClass1();创建实例,分别执行各块肋骨

发现来来来,测试一下C#的override和new_第2张图片

是这样的,只有方法1使用了紫类的方法,其余还是鸡肋的方法。

 

由于方法1覆盖(override)了原先的方法,因此即使转成父类的对象,仍旧调用了子类的F1方法

 

而其他方法new(隐藏)或无修饰了原先的方法,转成父类的对象,便隐藏不住了,然后露馅了。

 

看第三组测试和第一组测试结果一样,就会发现。

 

第二组由于实例就是紫类的,那方法当然只会执行紫类的方法了,至于6、7方法没有在紫类重写,那只好用爸爸的了

 

简单么,下期见

你可能感兴趣的:(编程,先学再说)