版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 建筑工程3-3建筑面积
- 护理课件制作软件的评测与最佳选择
- 2025年甲醇燃料船舶动力系统安全管理制度
- 四川省德阳市2026年中考模拟试题英语试卷(含答案无听力音频及原文)
- 护理课件制作工具的最佳选择与比较
- 冲压工安全风险强化考核试卷含答案
- 冷链食品安全管理员班组建设测试考核试卷含答案
- 玻璃钢制品灌注工岗前保密考核试卷含答案
- 2026年新科教版高中高一化学上册第一单元物质的量综合计算卷含答案
- 中药炮制工岗后考核试卷含答案
- 检验科个人防护培训课件
- 《XXXX煤矿隐蔽致灾地质因素普查报告》审查意见
- 行车培训教材地操培训
- 服装品质管理课件
- 现代物流学说课公开课一等奖市优质课赛课获奖课件
- 广东网架安装作业指导书四角锥网架
- 五年级数学下册第二单元检测卷4套+答案
- 16m预应力混凝土简支空心板桥设计
- GB/T 8305-2013茶水浸出物测定
- GB/T 4545-2007玻璃瓶罐内应力试验方法
- GB/T 31391-2015煤的元素分析
评论
0/150
提交评论