博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# 关于类的事件和委托
阅读量:4488 次
发布时间:2019-06-08

本文共 4481 字,大约阅读时间需要 14 分钟。

事件是C#中的又一个重要概念,在发生其他类或对象需要关注的事情时,

本类或对象可以通过事件来通知它们。

发送事件的类称为事件的发送者,而接受事件的类称为事件的订阅户。

C# 委托

委托使用的关键字为delegate

class Program    {        public delegate void Mypaly(string str);  //声明委托        class Play        {            private string game;            public Play(string s)            {                game = s;            }            public void PlayStart(string name)   //委托调用的方法1            {                Console.WriteLine("“{0}”开始玩《{1}》!", name, game);            }            public void PlayClose(string name)     //委托调用的方法2            {                Console.WriteLine("“{0}”已关掉《{1}》!", name, game);            }            public void callMypaly(Mypaly d, string s)  //调用委托中的方法            {                d(s);            }        }        static void Main(string[] args)        {            Play paly = new Play("魔兽争霸");            Mypaly mypalyStart = new Mypaly(paly.PlayStart);            Mypaly mypalyClose = new Mypaly(paly.PlayClose);            Mypaly mypaly = mypalyStart + mypalyClose;            mypaly("张三");            //paly.callMypaly(mypalyStart, "张三");            //paly.callMypaly(mypalyClose, "张三");            Console.ReadKey();        }    }

 

打印结果:

“张三”开始玩《魔兽争霸》!

“张三”已关掉《魔兽争霸》!

 

C# 事件中的委托

声明事件的关键字为event

class Program    {        class Eventclass  //先定义一个类        {            public delegate void CustomEventHandler(object sender, EventArgs e); //⒈定义一个委托类型的对象            //用delegate数据类型声明事件,要用event关键字            public event CustomEventHandler CustomEvent;            public event CustomEventHandler Load;            public void InCustomEvent()   //引用事件方法            {                CustomEvent(this, EventArgs.Empty);            }            public void onLoad()            {                temp1:                Console.BackgroundColor = ConsoleColor.Red;                Load(this, EventArgs.Empty);                string s = Console.ReadLine();                if (s != "yuping")                {                    Console.WriteLine("You must type 'yuping' for change it !");                    goto temp1;                }                else                {                    Console.BackgroundColor = System.ConsoleColor.Black;                    Console.Clear();                }            }            public void CustomEvent1(object sender, EventArgs e)            {                Console.WriteLine("Fire Event");            }            public void Load1(object sender, EventArgs e)            {                Console.WriteLine("Current background color is {0}. Please input:", System.Console.BackgroundColor.ToString());            }        }        static void Main(string[] args)        {            Eventclass myEventclass = new Eventclass(); //实例Eventclass类            myEventclass.CustomEvent += new Eventclass.CustomEventHandler(myEventclass.CustomEvent1); //⒉实例关联事件            myEventclass.InCustomEvent();  //⒊调用            myEventclass.Load += new Eventclass.CustomEventHandler(myEventclass.Load1);            myEventclass.onLoad();            Console.ReadKey();        }    }

 

打印结果:

Fire Event

Current background color is Red. Please input:

abc

You must type 'yuping' for change it !

Current background color is Red. Please input:

//举例:通过在Class2中定义并触发事件,在Class1中调用事件处理程序。    class Program    {        public class MyEventArgs : EventArgs  //继承EventArgs 包含事件数据的类的基类        {            public string str;        }        public delegate void MyEventHandler1(object sender, MyEventArgs e); //声明委托对象        class Class2        {            public event MyEventHandler1 Event1; //定义事件            public void mEvent1(MyEventArgs e)  //触发事件            {                if (Event1 != null)                {                    Event1(this, e);                }            }        }        class Class1        {            public Class1(Class2 class2) //创建委托对象并包含事件处理函数            {                MyEventHandler1 mh1 = new MyEventHandler1(Method1);                class2.Event1 += mh1; //订阅事件   这里只能+= 或-=            }            public void Method1(object sender, MyEventArgs e)            {                Console.WriteLine("事件处理结果:" + e.str);            }        }        static void Main(string[] args)        {            Class2 class2 = new Class2();            Class1 class1 = new Class1(class2);            MyEventArgs my1 = new MyEventArgs();            my1.str = "aaa";            class2.mEvent1(my1);            Console.ReadKey();        }    }

 

转载于:https://www.cnblogs.com/han1982/archive/2012/09/17/2689809.html

你可能感兴趣的文章
django项目部署在Apache服务器中,静态文件路径的注意点
查看>>
转:objective-c 协议和委托
查看>>
day 55 jQuery 之事件 绑定等
查看>>
前端开源项目周报0221
查看>>
虚机克隆搭建kafka服务器集群
查看>>
Laravel-lumen 配置JWT
查看>>
MySQL常用存储引擎:MyISAM与InnoDB之华山论剑
查看>>
MVC5+EF6 --自定义控制Action访问权限
查看>>
[CF786B] Legacy
查看>>
Spring 注解@Component,@Service,@Controller,@Repository
查看>>
设置RDLC中table控件的表头在每页显示
查看>>
linux中tomcat内存溢出解决办法 分类: 测试 ...
查看>>
jQuery $.each用法
查看>>
[Luogu 3902]Increasing
查看>>
clear语句处理不同类型的数据结果
查看>>
HDU 6118 度度熊的交易计划(费用流)
查看>>
UrlEncode编码/UrlDecode解码使用方法
查看>>
使用ubuntu作为web开发环境的一些感受
查看>>
easyui-datagrid 自适应列宽问题
查看>>
OO第一次总结
查看>>