




已阅读5页,还剩4页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
实验四 类的设计和实现(2)实验学时:2学时实验类型:验证实验要求:必做一、实验目的1. 掌握类的继承的实现;2. 掌握派生类、抽象类、抽象方法的使用;3. 了解接口的实现;4. 了解事件的实现二、实验内容实验1类的继承的实现实验要求:参照课本例8.3,创建基类Person和派生类Teacher。基类Person包含实例字段name和age;虚函数GetInfo()显示个人信息。派生类Teacher除了包含基类的name和age字段,还包含自己的TeacherID字段,并使用关键字override来重写方法GetInfo()。源程序:using System;using System.Collections.Generic;using System.Text;namespace ConsoleApplication12 public class Person public string name; public uint age; public Person(string name, uint age) = name; this.age = age; public virtual void GetInfo() Console.WriteLine(Name:0, name); Console.WriteLine(Age:0, age); public class Teacher : Person public string TeacherID; public Teacher(string name, uint age, string id) : base(name, age) this.TeacherID = id; public override void GetInfo() base.GetInfo(); Console.WriteLine(TeacherID:0, TeacherID); class Program static void Main(string args) Teacher objteacher = new Teacher(Zhangying, 20, 1245713131); objteacher.GetInfo(); Console.ReadKey(); 运行结果:实验2 抽象类、抽象方法、多态性的实现实验要求:创建抽象基类Shape和派生类Rectangle、Circle。利用多态性实现Area(计算面积)和Show(显示图形名称和面积)抽象方法。源程序:using System;using System.Collections.Generic;using System.Text;namespace ConsoleApplication13 public abstract class Shape private string m_id; public Shape(string s) Id= s; public string Id get return m_id; set m_id = value; public abstract double Area get; public override string ToString() return Id + Area= + string.Format(0:F2, Area); public class Rectangle : Shape private int m_width; private int m_height; public Rectangle(int width, int height, string id) : base(id) m_width = width; m_height = height; public override double Area get return m_height * m_width; public class Circle : Shape private int m_radius; public Circle(int radius, string id) : base(id) m_radius = radius; public override double Area get return m_radius * m_radius * System.Math.PI; class Program static void Main(string args) Shape shapes= new Circle (3,Circle # 1), new Rectangle(4,5,Rectangle # 1) ; Console.WriteLine(Shape Colection); foreach (Shape s in shapes) Console.WriteLine(s); Console.ReadKey(); 运行结果:实验3 接口的实现实验要求:声明一个接口ICDPlayer,包含4个接口方法:Play()、Stop()、NextTrack()和PreviousTrack(),以及一个只读属性CurrentTrack创建类CDPlayer实现该接口,模拟CD的播放、停止、下一音轨、上一音轨的操作。(如何实现自己决定,比如可以直接在控制台输出“CD播放中”等等)。源程序:using System;using System.Collections.Generic;using System.Text;namespace ConsoleApplication15 public interface ICDPlayer void Play(); void Stop(); void NextTrack(); void PreviousTrack(); int CurrentTrack get; public class CDPlayer:ICDPlayer private int currentTrack = 0; public int CurrentTrack get return currentTrack; public void Play() Console.WriteLine(开始播放歌曲); Console.WriteLine(CD.CurrentTrack=0,currentTrack); public void Stop() Console.WriteLine (停止播放歌曲) ; public void NextTrack() Console.WriteLine(下一歌曲); currentTrack+; Console.WriteLine(CD.CurrentTrack=0,currentTrack); public void PreviousTrack() Console.WriteLine(前一首歌曲); if (currentTrack = 1) currentTrack-; Console.WriteLine(CD.CurrentTrack=0, currentTrack); static void Main(string args) string selection; Console.WriteLine(该程序控制歌曲播放!); Console.WriteLine(1.开始播放歌曲!); Console.WriteLine(2.停止播放歌曲!); Console.WriteLine(3.下一歌曲!); Console.WriteLine(4.前一首歌曲!); CDPlayer CD=new CDPlayer(); do Console.WriteLine(请选择:); selection = Console.ReadLine(); switch (selection) case 1: CD.Play(); break; case 2: CD.Stop(); break; case 3: CD.NextTrack(); break; case 4: CD.PreviousTrack(); break; while (selection !=2); Console.ReadKey(); 运行结果:实验4 事件的实现实验要求:参照课本例9.7的综合示例,实现事件处理。源程序:using System;using System.Collections;using System.Text;namespace ConsoleApplication14 public class NameListEventArgs : EventArgs private string name; public string Name get return ; set = value; private int count; public int Count get return this.count; set this.count = value; public NameListEventArgs(string name, int count) Name = name; Count = count; public delegate void NameListEventHandler(object source, NameListEventArgs args); public class NameList ArrayList list; public event NameListEventHandler nameListEvent; public NameList() list = new ArrayList(); public void Add(string Name) list.Add(Name); if (nameListEvent != null) nameListEvent(this, new NameListEventArgs(Name, list.Count); public class EventDemo public static void Method1(object source, NameListEventArgs args) Console.WriteLine(列表中增加了项目:0, args.Name); public static void Method2(object source, NameListEventArgs args) Console.WriteLine(列表中的项目数:0, args.Count); public static void Main() NameList n1 = new NameList(); ListEvent += new NameListEventHandler(EventDemo.Method1); ListEvent += new NameListEventHandler(EventDemo.Method2); n1.Add(张三
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 原木品质分类系统创新创业项目商业计划书
- 农作物无人机种植应用服务创新创业项目商业计划书
- 智能培训软件创新创业项目商业计划书
- 2025年汾阳市消防员考试笔试试题(含答案)
- 智能查验机器人创新创业项目商业计划书
- 辐射类仪器安全培训课件
- 2025年生态补偿机制在生态系统服务功能补偿中的应用报告
- 2025年纺织服装行业智能化生产智能化生产设备租赁行业政策分析报告
- 2025年模具制造数字化设计与仿真技术市场前景分析报告
- 现场做柜子施工知识培训课件
- 生猪屠宰加工项目可行性研究报告
- 劳动力、机械设备、材料投入计划
- 病人欠费催缴通知单
- GB/T 8627-2007建筑材料燃烧或分解的烟密度试验方法
- GB/T 3280-2015不锈钢冷轧钢板和钢带
- GB 19510.14-2009灯的控制装置第14部分:LED模块用直流或交流电子控制装置的特殊要求
- GA 576-2018防尾随联动互锁安全门通用技术条件
- 中职语文-病句修改专题复习【公开课】课件
- 企业突发环境事件隐患排查与治理工作指南课件
- 初中语文七年级现代文阅读理解精选:说明文20篇(含答案)
- DB21T 3515-2021 灌注式复合混凝土路面设计与施工技术规范
评论
0/150
提交评论