我们知道在C#中,基类声明为virtual和abstract的成员,在子类中可以被override,也就是重写。其实被加上override关键字的成员,它本身也是virtual的,可以继续被子类的成员override。
新建一个.NET Core控制台项目,敲入下面的代码:
using System; using System.Reflection; namespace ConsoleApp { public class A { public virtual void SampleMethod() { Console.WriteLine("A.SampleMethod"); } } public class B : A { public override void SampleMethod() { Console.WriteLine("B.SampleMethod"); } } public class C : B { public override void SampleMethod() { Console.WriteLine("C.SampleMethod"); } } class Program { static void Main(string[] args) { A a = new C(); a.SampleMethod(); B b = new C(); b.SampleMethod(); MethodInfo methodSampleMethodInA = typeof(A).GetMethod("SampleMethod", BindingFlags.Public | BindingFlags.Instance); Console.WriteLine("A.SampleMethod is virtual? {0}", methodSampleMethodInA.IsVirtual.ToString()); MethodInfo methodSampleMethodInB = typeof(B).GetMethod("SampleMethod", BindingFlags.Public | BindingFlags.Instance); Console.WriteLine("B.SampleMethod is virtual? {0}", methodSampleMethodInB.IsVirtual.ToString()); MethodInfo methodSampleMethodInC = typeof(C).GetMethod("SampleMethod", BindingFlags.Public | BindingFlags.Instance); Console.WriteLine("C.SampleMethod is virtual? {0}", methodSampleMethodInC.IsVirtual.ToString()); Console.WriteLine("Press any key to end..."); Console.ReadKey(); } } }
执行上面的代码,结果如下所示:
可以看到,我们在上面的代码中,定义了类A,类B继承类A,类C继承类B。类A的SampleMethod方法是virtual的,是虚方法。类B的SampleMethod方法使用override关键字,重写了类A的SampleMethod方法。类C的SampleMethod方法又使用override关键字,重写了类B的SampleMethod方法。我们在Main方法中,使用A a和B b来调用SampleMethod方法时,最后都调用的是类C的SampleMethod方法,因为A a和B b指向的都是类C的对象(new C()),并且类A和类B的SampleMethod方法,在继承链中最终都被类C的SampleMethod方法给重写(override)了。所以我们可以看到实际上被声明为override的成员,还可以继续被子类成员使用override关键字重写,这说明声明为override的成员本身就是virtual的,我们可以从上面Main方法的代码中看到,当使用反射来获取类A、类B、类C的SampleMethod方法时,这三个SampleMethod方法的IsVirtual属性都为true,这说明声明为virtual和override的方法,都是虚方法。
但是现在如果我们在类B和类C的SampleMethod方法上,使用new关键字来隐藏基类的SampleMethod方法,那么会得到完全不一样的结果,如下代码所示:
using System; using System.Reflection; namespace ConsoleApp { public class A { public virtual void SampleMethod() { Console.WriteLine("A.SampleMethod"); } } public class B : A { public new void SampleMethod() { Console.WriteLine("B.SampleMethod"); } } public class C : B { public new void SampleMethod() { Console.WriteLine("C.SampleMethod"); } } class Program { static void Main(string[] args) { A a = new C(); a.SampleMethod(); B b = new C(); b.SampleMethod(); MethodInfo methodSampleMethodInA = typeof(A).GetMethod("SampleMethod", BindingFlags.Public | BindingFlags.Instance); Console.WriteLine("A.SampleMethod is virtual? {0}", methodSampleMethodInA.IsVirtual.ToString()); MethodInfo methodSampleMethodInB = typeof(B).GetMethod("SampleMethod", BindingFlags.Public | BindingFlags.Instance); Console.WriteLine("B.SampleMethod is virtual? {0}", methodSampleMethodInB.IsVirtual.ToString()); MethodInfo methodSampleMethodInC = typeof(C).GetMethod("SampleMethod", BindingFlags.Public | BindingFlags.Instance); Console.WriteLine("C.SampleMethod is virtual? {0}", methodSampleMethodInC.IsVirtual.ToString()); Console.WriteLine("Press any key to end..."); Console.ReadKey(); } } }
执行上面的代码,结果如下所示:
当我们将类B和类C的SampleMethod方法,从override关键字改为new关键字后,A a调用的是类A的SampleMethod方法,B b调用的是类B的SampleMethod方法,并且类B和类C的SampleMethod方法通过反射得到的IsVirtual属性现在为false,说明类B和类C的SampleMethod方法,现在不是虚方法了。
参考文献: