您好,登录后才能下订单哦!
在前面的文章中,我们介绍了编译期多态、params关键字、实例化、base关键字等。本节我们来关注另外一种多态:运行时多态, 运行时多态也叫迟绑定。
在C#语音中,运行时多态也叫方法重写(overriding),我们可以在子类中overriding基类的同签名函数,使用“virtual & override”关键字即可。
创建一个console 示例工程,命名为InheritanceAndPolymorphism。在Program.cs基础上,再添加2个类文件,分别命名为ClassA.cs、ClassB.cs。拷贝如下代码:
public class ClassA { public void AAA() { Console.WriteLine("ClassA AAA"); } public void BBB() { Console.WriteLine("ClassA BBB"); } public void CCC() { Console.WriteLine("ClassA CCC"); } }
ClassB:
public class ClassB { public void AAA() { Console.WriteLine("ClassB AAA"); } public void BBB() { Console.WriteLine("ClassB BBB"); } public void CCC() { Console.WriteLine("ClassB CCC"); } }
在上面的代码中,我们可以看到ClassA、ClassB有同样签名的方法,可以在program.cs中直接使用。
我们对代码再做休整,结构如下:
/// <summary> /// ClassB, acting as a base class /// </summary> public class ClassB { public void AAA() { Console.WriteLine("ClassB AAA"); } public void BBB() { Console.WriteLine("ClassB BBB"); } public void CCC() { Console.WriteLine("ClassB CCC"); } } /// <summary> /// Class A, acting as a derived class /// </summary> public class ClassA : ClassB { public void AAA() { Console.WriteLine("ClassA AAA"); } public void BBB() { Console.WriteLine("ClassA BBB"); } public void CCC() { Console.WriteLine("ClassA CCC"); } }
Program.cs
/// <summary> /// Program: used to execute the method. /// Contains Main method. /// </summary> public class Program { private static void Main(string[] args) { ClassA x = new ClassA(); ClassB y=new ClassB(); ClassB z=new ClassA(); x.AAA(); x.BBB(); x.CCC(); y.AAA(); y.BBB();y.CCC(); z.AAA(); z.BBB(); z.CCC(); } }
F5,运行代码,结果如下:
ClassA AAA
ClassA BBB
ClassA CCC
ClassB AAA
ClassB BBB
ClassB CCC
ClassB AAA
ClassB BBB
ClassB CCC
但同时,在VS的Output窗口,我们获得了3个Warnings:
'InheritanceAndPolymorphism.ClassA.AAA()' hides inherited member
'InheritanceAndPolymorphism.ClassB.AAA()'. Use the new keyword if hiding was intended.
'InheritanceAndPolymorphism.ClassA.BBB()' hides inherited member
'InheritanceAndPolymorphism.ClassB.BBB()'. Use the new keyword if hiding was intended.
'InheritanceAndPolymorphism.ClassA.CCC()' hides inherited member
'InheritanceAndPolymorphism.ClassB.CCC()'. Use the new keyword if hiding was intended.
这些Warnings的原因是因为子类和基类的AAA、BBB、CCC方法签名相同,尽管从执行上看优先执行子类同签名的方法,但是可能会有潜在的问题,故Warnings提出。
基于上面的Warning,我们手动修改代码,看看如何消除这些Warnings。
先给子类添加new、override关键字试试:
/// <summary> /// Class A, acting as a derived class /// </summary> public class ClassA : ClassB { public override void AAA() { Console.WriteLine("ClassA AAA"); } public new void BBB() { Console.WriteLine("ClassA BBB"); } public void CCC() { Console.WriteLine("ClassA CCC"); } }
执行的结果是报错了:
Error: 'InheritanceAndPolymorphism.ClassA.AAA()': cannot override inherited member 'InheritanceAndPolymorphism.ClassB.AAA()' because it is not marked virtual, abstract, or override
从这个错误提示信息看,我们需要修改基类方法,如添加virtual关键字。
/// <summary> /// ClassB, acting as a base class /// </summary> public class ClassB { public virtual void AAA() { Console.WriteLine("ClassB AAA"); } public virtual void BBB() { Console.WriteLine("ClassB BBB"); } public virtual void CCC() { Console.WriteLine("ClassB CCC"); } } /// <summary> /// Class A, acting as a derived class /// </summary> public class ClassA : ClassB { public override void AAA() { Console.WriteLine("ClassA AAA"); } public new void BBB() { Console.WriteLine("ClassA BBB"); } public void CCC() { Console.WriteLine("ClassA CCC"); } } /// <summary> /// Program: used to execute the method. /// Contains Main method. /// </summary> public class Program { private static void Main(string[] args) { ClassB y = new ClassB(); ClassA x = new ClassA(); ClassB z = new ClassA(); y.AAA(); y.BBB(); y.CCC(); x.AAA(); x.BBB(); x.CCC(); z.AAA(); z.BBB(); z.CCC(); Console.ReadKey(); } }
执行,则无Warning了,通过这个实例,我们得知通过在基类添加Virtual关键字授权其子类可override基类同签名方法的权限,方便了OOP的扩展。
在ClassA\
ClassB基础上,下面添加
ClassC,看看3个类继承关系的运行时多态:
/// <summary> /// ClassB, acting as a base class /// </summary> public class ClassB { public void AAA() { Console.WriteLine("ClassB AAA"); } public virtual void BBB() { Console.WriteLine("ClassB BBB"); } public virtual void CCC() { Console.WriteLine("ClassB CCC"); } } /// <summary> /// Class A, acting as a derived class /// </summary> public class ClassA : ClassB { public virtual void AAA() { Console.WriteLine("ClassA AAA"); } public new void BBB() { Console.WriteLine("ClassA BBB"); } public override void CCC() { Console.WriteLine("ClassA CCC"); } } /// <summary> /// Class C, acting as a derived class /// </summary> public class ClassC : ClassA { public override void AAA() { Console.WriteLine("ClassC AAA"); } public void CCC() { Console.WriteLine("ClassC CCC"); } } /// <summary> /// Program: used to execute the method. /// Contains Main method. /// </summary> public class Program { private static void Main(string[] args) { ClassB y = new ClassA(); ClassB x = new ClassC(); ClassA z = new ClassC(); y.AAA(); y.BBB(); y.CCC(); x.AAA(); x.BBB(); x.CCC(); z.AAA(); z.BBB(); z.CCC(); Console.ReadKey(); } }
运行结果:
ClassB AAA
ClassB BBB
ClassA CCC
ClassB AAA
ClassB BBB
ClassA CCC
ClassC AAA
ClassA BBB
ClassA CCC
如果基类声明了virtual 关键字,子类可使用
override修饰符实现运行时多态:只有在编译器动态决定是否被调用。
如果未标明virtual或非virtual,则方法是否被调用在编译期就能决定。
再看看下面的例子:
internal class A { public virtual void X() { } } internal class B : A { public new void X() { } } internal class C : B { public override void X() { } }
F5运行,结果报错了:
Error: 'InheritanceAndPolymorphism.C.X()': cannot override inherited member 'InheritanceAndPolymorphism.B.X()' because it is not marked virtual, abstract, or override
错误的原因是A中定义了virtual的X函数,在B中用new关键字隐藏了A中的X函数。当C尝试通过override关键字的时候,是获得不了A中的virtual关键字X函数的,既在C中X函数为非Virtual的,故不能override。
internal class A { public virtual void X() { Console.WriteLine("Class: A ; Method X"); } } internal class B : A { public new virtual void X() { Console.WriteLine("Class: B ; Method X"); } } internal class C : B { public override void X() { Console.WriteLine("Class: C ; Method X"); } } /// <summary> /// Program: used to execute the method. /// Contains Main method. /// </summary> public class Program { private static void Main(string[] args) { A a = new C(); a.X(); B b = new C(); b.X(); Console.ReadKey(); } }
执行结果如下:
Class: A ; Method X Class: C ; Method X
在这里,我们通过在B类中添加new Virtual修饰符,然后在C中即可使用B中Virtual的X函数了。
在上面继承上,在运行时多态中添加第四个类:ClassD。
/// <summary> /// Class A /// </summary> public class ClassA { public virtual void XXX() { Console.WriteLine("ClassA XXX"); } } /// <summary> /// ClassB /// </summary> public class ClassB:ClassA { public override void XXX() { Console.WriteLine("ClassB XXX"); } } /// <summary> /// Class C /// </summary> public class ClassC : ClassB { public virtual new void XXX() { Console.WriteLine("ClassC XXX"); } } /// <summary> /// Class D /// </summary> public class ClassD : ClassC { public override void XXX() { Console.WriteLine("ClassD XXX"); } } /// <summary> /// Program: used to execute the method. /// Contains Main method. /// </summary> public class Program { private static void Main(string[] args) { ClassA a = new ClassD(); ClassB b = new ClassD(); ClassC c=new ClassD(); ClassD d=new ClassD(); a.XXX(); b.XXX(); c.XXX(); d.XXX(); Console.ReadKey(); } }
执行结果如下:
ClassB XXX
ClassB XXX
ClassD XXX
ClassD XXX
第一行输出中,来自a.XXX()函数 , 我们在 ClassA中定义了XXX函数,然后在ClassB中使用new关键字切断了virtual关系--对子类而言。因此XXX函数从ClassC开始成为新的virtual函数,在这个代码中a是ClassD的实例,但是声明的为ClassA,故从下往上找,找到ClassB的XXX函数,打印并输出结果。
/// <summary> /// Class A /// </summary> public class ClassA { public virtual void XXX() { Console.WriteLine("ClassA XXX"); } } /// <summary> /// ClassB /// </summary> public class ClassB:ClassA { public override void XXX() { ((ClassA)this).XXX(); Console.WriteLine("ClassB XXX"); } } /// <summary> /// Program: used to execute the method. /// Contains Main method. /// </summary> public class Program { private static void Main(string[] args) { ClassA a = new ClassB(); a.XXX(); } }
运行报错:
Error: {Cannot evaluate expression because the current thread is in a stack overflow state.}
在这个例子中,((ClassA)this).XXX(); 导致了循环调用,修改为base.XXX即可修复这个强转导致的循环调用。
在C#中,子类对象可赋值给一个基类对象;相反需要强转。
override关键字用于子类重写同签名的基类virtual函数
用new和override可重写基类virtual的同签名函数
virtual修饰符的函数,只能在运行时决定是否被执行
函数未用virtual修饰,则在编译期即可决定是否被调用
原文链接:Diving in OOP (Day 3): Polymorphism and Inheritance (Dynamic Binding/Run Time Polymorphism)
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。