预览加载中,请您耐心等待几秒...
1/10
2/10
3/10
4/10
5/10
6/10
7/10
8/10
9/10
10/10

亲,该文档总共36页,到这已经超出免费预览范围,如果喜欢就直接下载吧~

如果您无法下载资料,请参考说明:

1、部分资料下载需要金币,请确保您的账户上有足够的金币

2、已购买过的文档,再次下载不重复扣费

3、资料包下载后请先用软件解压,在使用对应软件打开

本讲内容一、虚方法与方法覆写型与值的问题开拖拉机与开汽车的故事开拖拉机与开汽车的故事(续)验证一下“不变应万变”的幕后英雄从“继承”说起usingSystem; publicclassBase { publicvirtualvoidSayHello() { Console.WriteLine("Hello!"); } } publicclassDerived:Base { } publicclassClient { publicstaticvoidMain() { Derivedd=newDerived(); d.SayHello(); } }usingSystem; publicclassBase { publicvirtualvoidSayHello() { Console.WriteLine("Hello!"); } } publicclassDerived:Base { publicoverridevoidSayHello() { Console.WriteLine(“NewHello!"); } }publicclassVehicle { publicvirtualvoidDrive(); } publicclassCar:Vehicle { } publicclassTruck:Car { }二、虚拟方法表什么是虚拟方法表虚拟方法表特点三、继承与方法覆写的本质继承案例分析usingSystem; publicclassVehicle { publicintwheels; publicintspeed; publicvirtualvoidDrive(){ Console.WriteLine("DriveVehicle."); } publicvirtualvoidStop(){ Console.WriteLine("StopVehicle."); } } publicclassMotorcycle:Vehicle { publicoverridevoidDrive(){ Console.WriteLine("DriveMotorcycle."); } publicoverridevoidStop(){ Console.WriteLine("StopMotorcycle."); } } publicclassCar:Vehicle { publicintmaxSpeed; publicoverridevoidDrive(){ Console.WriteLine("DriveCar."); } publicvirtualvoidSetMaxSpeed(intmaxSpeed){ this.maxSpeed=maxSpeed; Console.WriteLine("Setcarmaxspeedto{0}.",maxSpeed); } }VMTofVehicleVehicle四、多态的本质多态Vehicle虚方法的本质publicstaticvoidMain() { Drive(newTruck()); Drive(newCar()); } staticvoidDrive(Vehiclev) { v.Drive(); }方法小结强制类型转换Vehicle使用new关键字隐藏方法使用new关键字隐藏方法(续)作业:写出下面程序运行结果五、应用举例以不变应万变中的“马脚”反射机制可以解决神奇的插件式应用开发小结小结