面向对象第五章继承和多态.ppt_第1页
面向对象第五章继承和多态.ppt_第2页
面向对象第五章继承和多态.ppt_第3页
面向对象第五章继承和多态.ppt_第4页
面向对象第五章继承和多态.ppt_第5页
已阅读5页,还剩18页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、第5章 继承和多态,5.1继承,基类和派生类 自顶向下的分解 自底向上的抽象,图形,飞机,汽车,轮船,交通工具,5.1继承,基类和派生类 .NET类库,5.1继承,派生类的定义,public class Automobile private float speed = 100; public float Run(float distance) return distance/speed; public class Bus : Automobile ,5.1继承,派生类的使用,public static void Main() Automobile a1 = new Automobile();

2、a1.Run(1000); Bus b1 = new Bus(); b1.Run(1000); a1 = new Bus(); ,例:P5_1,5.1继承,隐藏基类成员,public class Automobile protected float speed; protected float weight; public float Run(float distance) return distance/speed; ,5.1继承,隐藏基类成员,public class Car : Automobile public new float Run(float distance) return

3、(1+weight/100) * distance/speed; ,5.1继承,隐藏基类成员 覆盖:根据声明类型决定成员调用,Automobile a1 = new Car(); a1.Run(1000); Car a2 = new Car(); a2.Run(1000);,Automobile.Run,Car.Run,例:P5_2,5.1继承,base关键字,public class Car:Automobile() . Public void ShowSpeed() Console.WriteLine(“理想状态行驶1000千米需要0小时”,base.Run(1000); Console.

4、WriteLine(“行驶1000千米需要0小时”, Run(1000); ,Automobile.Run,Car.Run,public Truck (float speed):base(speed,15) load = 30; ,5.1继承,base关键字,public Truck (float speed):base(speed) load = 30; Weight = 15; ,public Automobile() public Automobile(float speed) this.speed = speed; public Automobile(float speed,float

5、 weight) this.speed = speed; this.weight = weight; ,5.1继承,对象的生命周期,public class Vehicle public class Automobile : Vehicle public class Car : Automobile public class Limousine : Car ,new Vehicle(),new Automobile(),new Car(),new Limousine(),Vehicle(),Automobile(), Car(),Limousine(),例:P5_3,5.2多态性,If(a1

6、is Bus) Console.WriteLine(“客车行驶1000千米需0小时”,(Bus)a1).Run(1000); else if (a1 is Truck) Console.WriteLine(“客车行驶1000千米需0小时”,(Truck)a1).Run(1000); else Console.WriteLine(“客车行驶1000千米需0小时”,a1.Run(1000);,问题: 假设Bus和Truck类继承Automobile,并且都隐藏了Run方法,如何准确计算某汽车对象a1的行驶时间?,5.2多态性,基类:虚拟成员,public class Automobile prot

7、ected float speed; protected float weight; public virtual float Run(float distance) return distance/speed; ,5.2多态性,派生类:重载成员,public class Truck : Automobile private float load; public override float Run(float distance) return base.Run(1+(load+weight)/100); ,public class Car : Automobile public overri

8、de float Run(float distance) return (1+weight/100) * distance/speed; ,5.2多态性,重载:根据实际类型决定成员调用,Automobile a1 = new Car(); a1.Run(1000); Automobile a2 = new Truck(); a2.Run(1000);,Car.Run,Truck.Run,例:P5_4,5.2多态性,抽象类和抽象方法 抽象类,public abstract class Automobile protected float speed; protected float weight

9、; public float Run(float distance) return distance/speed; ,Automobile a1 = new Automobile();,5.2多态性,抽象类和抽象方法 抽象类,public abstract class Automobile protected float speed; protected float weight; public virtual float Run(float distance) return 0; ,5.2多态性,抽象类和抽象方法 抽象方法,public abstract class Automobile p

10、rotected float speed; protected float weight; public abstract float Run(float distance); ,public class Truck : Automobile private float load; ,5.2多态性,抽象类和抽象方法 抽象方法,public class Truck : Automobile private float load; public override float Run(float distance) return base.Run(1+(load+weight)/100); ,5.2

11、多态性,抽象类和抽象方法 抽象属性和索引函数,public abstract int X get; ,public abstract int thisint index get;set; ,例:P5_5,5.2多态性,密封类,public class BigTruck : Truck ,public sealed class Truck : Automobile private float load; public override float Run(float distance) return base.Run(1+(load+weight)/100); ,5.2多态性,密封方法,public class BigTruck : Truck public override float Run(float distance) return base.Run(1+2*(load+weight)/100); ,public class Truck : Automobile private float load; public sealed override float Run(float distance) return base.Run(1+(lo

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论