我的C#实验二---多态,事件
面向对象的设计思想(控制台程序)
实验二
【实验目的】
熟练掌握面向对象程序设计的概念,如类、对象、继承、多态、封装及其数据隐藏等,熟练掌握类的定义与使用,能够利用面向对象技术进行程序设计。
【实验要求】
1.是一个基于SDK开发的实现面向对象技术的控制台应用程序,功能自定义。
2.要体现面向对象程序设计的概念,如类、对象、继承、多态、封装及其数据隐藏等。
【实验步骤】(要求自己填写详细的实验步骤,设计思路和关键代码)
【实验体会及存在问题】要求自己填写,感想、设计时碰到的问题,包括设计思想、调试等)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | namespace 实验二 { //建立一个封装事件所需要的数据的类OnEventKeyArgs public class OnEventKeyArgs : EventArgs { private char word; public OnEventKeyArgs( char word) : base () { this .word = word; } public char Word { get { return word; } } } //事件处理程序 public class EventKeyHanderClass { public void EventKeyFound( object sender, OnEventKeyArgs EventKeyEventArgs) { Console.WriteLine(EventKeyEventArgs.Word); } } //声明一个名为EventKeyHandler的标准委托 public delegate void EventKeyHandler( object sender, OnEventKeyArgs EventKeyArgs); public class Char { //声明一个事件OnEventKey public event EventKeyHandler OnEventKey; public Char() { OnEventKey = null ; } public virtual void print() { Console.WriteLine( "我是个字母" ); } } public class A : Char { public event EventKeyHandler OnEventKey; private char a = 'A' ; public override void print() { Console.WriteLine( "我是字母A" ); OnEventKey( this , new OnEventKeyArgs(a)); } public char getA { get { return a; } } } public class B : Char { private char b = 'B' ; public char getB { get { return b; } } public override void print() { Console.WriteLine( "这是字母B" ); } } } |
代码入口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | namespace 实验二 { class Program { public static void AddChar(Char c) { c.print(); } static void Main( string [] args) { Char c = new Char(); A a = new A(); B b = new B(); c.OnEventKey += new EventKeyHandler( new EventKeyHanderClass().EventKeyFound); a.OnEventKey += new EventKeyHandler( new EventKeyHanderClass().EventKeyFound); b.OnEventKey += new EventKeyHandler( new EventKeyHanderClass().EventKeyFound); //c.input();//开始->触发事件 AddChar(a); AddChar(b); AddChar(c); } } } |
感觉王育齐的实验报告很难,因为他只提供要求,不像以前赵红直接要求我们做什么题目。。。但我还是挺喜欢这个老师的,因为他讲课会笑。。轻松。。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南