在开发过程中,我们为了让一个类更有生命力,有时会用virtual来修饰一个方法好让子类来覆写它。但是如果有更新的子子类来覆写时,我们又不想让其影响到上一层的覆写,这时候就要用到new virtual来阻断覆写了。
public class Animal { public virtual void WhoAmI() { Console.WriteLine("I am Animal."); } } public class Dog : Animal { public override void WhoAmI() { Console.WriteLine("I am Dog."); } } public class Dog1 : Animal { public new void WhoAmI() { Console.WriteLine("I am Dog1."); } } public class HomeDog : Dog { /// <summary> /// 采用new virtual来修饰来阻断覆写 /// </summary> public new virtual void WhoAmI() { Console.WriteLine("I am HomeDog."); } } public class HuskyDog : HomeDog { public override void WhoAmI() { Console.WriteLine("I am HuskyDog."); } } [TestFixture] public class RunTest { [Test] public void NewAndOverrideRun() { Dog1 x = new Dog1(); x.WhoAmI(); //I am Dog1. ((Animal)x).WhoAmI();//I am Animal. //new修饰的方法调用,取决于类型。 } [Test] public void OverrideRun() { Dog x = new Dog(); x.WhoAmI();//I am Dog. var x1 = x as Animal; x1.WhoAmI();//I am Dog. //override修饰符的方法调用,取决于实例化对象的类型 } [Test] public void NewVirtualRun() { var x = new HomeDog(); x.WhoAmI();//I am HomeDog. ((Dog)x).WhoAmI();//I am Dog. ((Animal)x).WhoAmI();//I am Dog. //new virtual阻断了方法的向上覆写。并且,Dog的覆写不受影响 } [Test] public void NewVirtualThenOverrideRun() { var x = new HuskyDog(); x.WhoAmI();//I am HuskyDog. ((HomeDog)x).WhoAmI();//I am HuskyDog. ((Dog)x).WhoAmI();//I am Dog. ((Animal)x).WhoAmI();//I am Dog. //new virtual 阻断向上的覆写,但是新的子类覆写它时不和原来覆写结果一样。 } }
时间: 2024-11-03 04:41:21