深入浅出OOP(三):多态和继承(动态绑定/运行时多态)
在前面的文章中,我们介绍了编译期多态、params关键字、实例化、base关键字等。本节我们来关注另外一种多态:运行时多态, 运行时多态也叫迟绑定。
十余年建站经验, 做网站、成都网站设计客户的见证与正确选择。创新互联提供完善的营销型网页建站明细报价表。后期开发更加便捷高效,我们致力于追求更美、更快、更规范。
运行时多态或迟绑定、动态绑定
在C#语音中,运行时多态也叫方法重写(overriding),我们可以在子类中overriding基类的同签名函数,使用“virtual & override”关键字即可。
C#的New、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中直接使用。
我们对代码再做休整,结构如下:
////// ClassB, acting as a base class /// public class ClassB { public void AAA() { Console.WriteLine("ClassB AAA"); } public void BBB() { Console.WriteLine("ClassB BBB"); } public void CCC() { Console.WriteLine("ClassB CCC"); } } ////// Class A, acting as a derived class /// 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
////// Program: used to execute the method. /// Contains Main method. /// 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关键字试试:
////// Class A, acting as a derived class /// 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关键字。
////// ClassB, acting as a base class /// 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"); } } ////// Class A, acting as a derived class /// 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"); } } ////// Program: used to execute the method. /// Contains Main method. /// 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的扩展。
3个类的运行时多态
在ClassA\
ClassB基础上,下面添加
ClassC,看看3个类继承关系的运行时多态:
////// ClassB, acting as a base class /// 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"); } } ////// Class A, acting as a derived class /// 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"); } } ////// Class C, acting as a derived class /// public class ClassC : ClassA { public override void AAA() { Console.WriteLine("ClassC AAA"); } public void CCC() { Console.WriteLine("ClassC CCC"); } } ////// Program: used to execute the method. /// Contains Main method. /// 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"); } } ////// Program: used to execute the method. /// Contains Main method. /// 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函数了。
4个类的运行时多态
在上面继承上,在运行时多态中添加第四个类:ClassD。
////// Class A /// public class ClassA { public virtual void XXX() { Console.WriteLine("ClassA XXX"); } } ////// ClassB /// public class ClassB:ClassA { public override void XXX() { Console.WriteLine("ClassB XXX"); } } ////// Class C /// public class ClassC : ClassB { public virtual new void XXX() { Console.WriteLine("ClassC XXX"); } } ////// Class D /// public class ClassD : ClassC { public override void XXX() { Console.WriteLine("ClassD XXX"); } } ////// Program: used to execute the method. /// Contains Main method. /// 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函数,打印并输出结果。
永无止境的循环
////// Class A /// public class ClassA { public virtual void XXX() { Console.WriteLine("ClassA XXX"); } } ////// ClassB /// public class ClassB:ClassA { public override void XXX() { ((ClassA)this).XXX(); Console.WriteLine("ClassB XXX"); } } ////// Program: used to execute the method. /// Contains Main method. /// 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)
本文题目:深入浅出OOP(三):多态和继承(动态绑定/运行时多态)
链接URL:http://pwwzsj.com/article/ppcigg.html