C#多态、接口、委托及事件.doc_第1页
C#多态、接口、委托及事件.doc_第2页
C#多态、接口、委托及事件.doc_第3页
C#多态、接口、委托及事件.doc_第4页
C#多态、接口、委托及事件.doc_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

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

文档简介

太原工业学院计算机工程系实 验 报 告课程名称面向对象程序设计班级实验日期2015-04-22姓 名学号实验成绩实验名称多态、接口、委托及事件实验目的及要求1、掌握类的多态特性以及多态的实现方法。2、掌握接口的声明、实现方法,能够通过接口实现简单系统功能的组织。3、理解委托的作用,能够通过委托实现方法作为参数的传递。4、理解事件的含义和在程序设计中的作用,能够定义一个用户事件并实现事件的触发。实验环境Microsoft Visual Studio 2010实验内容1. 设计程序,实现类的继承和多态2. 设计程序,实现接口的应用3. 设计程序,实现委托的应用4. 设计程序,实现事件的应用 写了四个小程序,简单的实现了以上的内容算法描述及实验步骤1 类的继承和多态在实例中,首先添加一个父类Person类,再添加一个Teacher子类和Student子类都继承于Person类,分别重写父类中的抽象方法。最后在Program类中实例化两个对象,实现多态和继承。2 接口的应用 在实例中,首先添加一个接口Iperson,再添加一个Teacher类和Student类都继承接口Iperson,并且都实现接口Iperson里的抽象方法。最后在Program类中实例化两个对象,实现接口的应用。3 委托的应用 在实例中,首先添加一个父类Person类,再添加一个Teacher子类和Student子类都继承于Person类,分别重写父类中的抽象方法。最后在Program类中实例化两个对象,声明一个委托function,讲子类的方法绑定到function上,再调用function。4 事件的应用在实例中,首先添加一个父类Person类,再添加一个Teacher子类和Student子类都继承于Person类,分别重写父类中的抽象方法。在Student子类中写ChuFen事件。最后在Program类中实例化两个对象,调用事件。这个例子写事件不是很恰当,自己在事件这一块也不是很熟悉,有不足之处和不理解的地方。调试过程及实验结果1 类的继承和多态(调试结果) 2 接口(调试结果) 3 委托(调试结果) 4 事件(调试结果) 总结我通过这次的学习,基本掌握了多态,接口,委托还有事件的应用,以上的实例都是非常简单的应用,功能比较单一,都是为了便于学洗了理解。需要多多练习。对于委托和事件,我的理解还不够透彻,在实现事件那一块,不熟悉,也不太理解。手动能力很重要,多练多写,出现问题,自己努力尝试解决问题。附录1 类的继承和多态(代码)using System;using System.Collections.Generic;using System.Linq;using System.Text;/person类namespace myWork_20130523 public abstract class Person public string name; public abstract void Eat(); public abstract void Sleep(); public abstract void Work(); using System;using System.Collections.Generic;using System.Linq;using System.Text;/老师类namespace myWork_20130523 class Teacher:Person public Teacher(string name) = name; public override void Sleep() Console.WriteLine(name+老师会睡觉); public override void Eat() Console.WriteLine(name+老师会吃); public override void Work() Console.WriteLine(name+老师的工作:教学); using System;using System.Collections.Generic;using System.Linq;using System.Text;/学生类namespace myWork_20130523 class Student:Person public Student(string name) = name; public override void Eat() Console.WriteLine(学生+name+会吃饭); public override void Sleep() Console.WriteLine(学生+name+会睡觉); public override void Work() Console.WriteLine(学生+name+的工作是:学习); using System;using System.Collections.Generic;using System.Linq;using System.Text;/多态的实现namespace myWork_20130523 class Program public static void Main(string args) Person t = new Teacher(王大伟); t.Eat(); t.Sleep(); t.Work(); Person s = new Student(小明); s.Eat(); s.Sleep(); s.Work(); 2 接口的应用(代码)using System;using System.Collections.Generic;using System.Linq;using System.Text;/接口Ipersonnamespace myInterface_20130523 interface Iperson void Eat(); void Sleep(); void Hobby(); using System;using System.Collections.Generic;using System.Linq;using System.Text;/Teacher类namespace myInterface_20130523 class Teacher:Iperson public void Eat() Console.WriteLine(C#老师爱吃土豆); public void Sleep() Console.WriteLine(C#老师会睡觉); public void Hobby() Console.WriteLine(C#老师的爱好是骑单车); Console.WriteLine(=); using System;using System.Collections.Generic;using System.Linq;using System.Text;/student类namespace myInterface_20130523 class Student:Iperson public void Eat() Console.WriteLine(学生喜欢吃雪糕); public void Sleep() Console.WriteLine(学生会睡觉); public void Hobby() Console.WriteLine(学生的爱好是玩游戏和开发游戏); using System;using System.Collections.Generic;using System.Linq;using System.Text;/接口的实现namespace myInterface_20130523 class Program static void Main(string args) Teacher t = new Teacher(); t.Eat(); t.Sleep(); t.Hobby(); Student s = new Student(); s.Eat(); s.Sleep(); s.Hobby(); 3 委托(代码)using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace myDelegate_20130523 abstract class Person public abstract void Eat(); public abstract void Hobby(); using System;using System.Collections.Generic;using System.Linq;using System.Text;/Teacher类namespace myDelegate_20130523 class Teacher:Person public override void Eat()/方法的重写 Console.WriteLine(C#老师喜欢吃地三鲜); public override void Hobby() Console.WriteLine(C#老师的爱好是摄影); using System;using System.Collections.Generic;using System.Linq;using System.Text;/Student类namespace myDelegate_20130523 class Student:Person public override void Eat()/方法的重写 Console.WriteLine(学生喜欢吃大闸蟹); public override void Hobby() Console.WriteLine(学生的爱好是旅游); using System;using System.Collections.Generic;using System.Linq;using System.Text;/委托namespace myDelegate_20130523 class Program public delegate void delegatePerson(); static void Main(string args) Teacher t = new Teacher(); Student s = new Student(); delegatePerson function ;/声明一个委托 function = t.Eat;/把老师的Eat绑定到function function += t.Hobby;/把老师的Hobby继续绑定到function function += s.Eat;/把学生的Eat绑定到function function += s.Hobby;/把学生的Hobby继续绑定到function function();/调用function 4 事件(源代码)using System;using System.Collections.Generic;using System.Linq;using System.Text;/People类namespace myEvent_20130523 abstract class People public abstract void Event_P(); using System;using System.Collections.Generic;using System.Linq;using System.Text;/Teacher类namespace myEvent_20130523 class Teacher:People public override void Event_P() Console.WriteLine(老师发现小明迟到了); using System;using System.Collections.Generic;using System.Linq;using System.Text;/Student类namespace myEvent_20130523 class Student:People public int lateTime; public delegate void Event_S();/委托 public event Event_S ChuFen;/ChuFen事件 public override void Event_P() Console.WriteLine(小明迟到了); for (int i = 1; i 3) ChuFen();/处分事件 Console.WriteLine(小明第+i+次迟到,要接受处罚,做0个俯卧撑 ,i *5); else Console.WriteLine(小明第+i+次迟到,老师原谅了他,让小明进了教室

温馨提示

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

评论

0/150

提交评论