委托和事件

来源:互联网 发布:michael angelo知乎 编辑:程序博客网 时间:2024/06/11 09:32
委托和事件事例
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication6{    class Program    {        public delegate void degata1();//定义委托        public event degata1 function1; //定义事件         static void Main(string[] args)        {            degata1 mydelate = delegate()//匿名委托写法            {            };            Program p = new Program();            //p.function1 += new degata1(new b().myfun1);            p.function1 += () => new b().myfun1();            p.function1+=new degata1(new c().myfun2);            d d = new d();            d.event2 += new d.delegate2(new e().past);             d.event2+=new d.delegate2(new f().lambda);                       //d.event2 += (sender) => new e().past();            p.runevent();            d.runevent2();        }        public void runevent()        {            function1();        }    }    class b    {        public void myfun1()        {            Console.WriteLine("这是第一个测试方法!");        }    }    class c    {        public void myfun2()//订阅class program的事件        {            d myd = new d();            myd.event2+=new d.delegate2(new e().past);            myd.runevent2();        }    }    class d    {        public delegate void delegate2(object sender);///定义一个委托和事件        public event delegate2 event2;        public void runevent2()        {            if (event2 != null)            {                event2(this);            }        }    }    class e    {        public void past(object sender)//订阅class d的事件        {            Console.WriteLine("{0}",sender);            Console.WriteLine("这是第三个测试方法!");        }    }    class f    {        public void lambda(object sender)//订阅class d的事件        {            d myd2 = new d();            myd2.event2 += (sender1) =>new e().past(this);            myd2.runevent2();        }    }}//个人总结//事件必须先定义委托,然后定义事件,名称必须是事件关键字加上自己定义的委托名称在加上要定义的事件名。//匿名事件和匿名委托//在一个类里面定义一个委托和事件,事件的实现方法可以自定义在本类中或者其他类中//其他类如果需要使用该事件则必须订阅,可以自行提供事件触发后所执行的方法,可以自己定义该事件的触发方法
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication7{    ///     /// 定义委托和事件    ///     public class del    {              public delegate void dele2(int a, int b);//定义一个无返回值有2个int 参数的委托        public delegate void dele3(string c);//定义一个无返回值只有1个string参数的委托        public delegate int dele4(int d, int e);//定义int返回值且有2个int参数的委托        public event dele2 function;        public void deltest(int a, int b)        {            function += new dele2(addnum);            function(a,b);//事件触发点(必须在事件定义的类里面)        }        public void addnum(int a, int b)//当事件functions被触发后执行的方法        {            Console.WriteLine("a+b={0}",a+b);            Console.ReadLine();        }        public event dele3 delestr;        public void str(string s)        {            Console.WriteLine("{0}",s);            Console.ReadLine();        }        public void runeventstr(string s)        {            delestr(s);        }        public event dele4 delelast;        public int four(int a, int b)//dele4的绑定事件        {            return a * b;        }        public void fourstart(int a, int b)//dele4的触发事件        {            Console.WriteLine(delelast(a,b));            Console.ReadLine();        }    }}
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication7{    class Program    {        public delegate void dele1();//定义一个无返回值无参数的委托        static void Main(string[] args)        {            //dele1 del = new dele1(new test1().testDel);            dele1 del = ()=> new test1().testDel();//lambda表达式             del += delegate()//匿名方法            {                new test1().testDel();            };            del();            test2 t2 = new test2();            t2.rundele2();            test3 t3 = new test3();            t3.rundele3();            test4 t4 = new test4();            t4.del4();        }    }    ///     /// 委托    ///     class test1    {        public void testDel()        {            Console.WriteLine("这是一个委托试验方法!");        }    }    ///     /// 订阅dele2的方法    ///     class test2    {        del de = new del();        public void rundele2()        {            int num1=0;            int num2=0;            Console.WriteLine("输入数字1");            if (!int.TryParse(Console.ReadLine(), out num1))            {                Console.WriteLine("输入错误,请重新输入!");            }            Console.WriteLine("输入数字2");            if (!int.TryParse(Console.ReadLine(), out num2))            {                Console.WriteLine("输入错误,请重新输入!");            }            de.function+=new del.dele2(new del().deltest);            //de.function += (int i, int j) => new del().addnum(num1,num2);//lambda表达式法            new del().deltest(num1,num2);        }     }    class test3    {        del d = new del();        public void rundele3()        {            Console.WriteLine("请输入字符");            string s = Console.ReadLine();            //d.delestr += new del().str;            d.delestr += (string t) => new del().str(s);//lambda表达式            d.runeventstr(s);        }     }    class test4    {        del d=new del();        public void del4()        {            int num1 = 0;            int num2 = 0;            Console.WriteLine("输入数字1");            if (!int.TryParse(Console.ReadLine(), out num1))            {                Console.WriteLine("输入错误,请重新输入!");            }            Console.WriteLine("输入数字2");            if (!int.TryParse(Console.ReadLine(), out num2))            {                Console.WriteLine("输入错误,请重新输入!");            }            //d.delelast += new del.dele4(new del().four);            d.delelast += (int i, int j) => new del().four(num1,num2);//lambda表达式            d.fourstart(num1,num2);
原创粉丝点击