A10_委托基础回顾

来源:互联网 发布:农村淘宝快递网点查询 编辑:程序博客网 时间:2024/06/15 08:10

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace A10_IndepthStudyDelegate{    //定义委托    public delegate void InformInfoHandler(string name);    class Invoke    {        //声明事件(委托实例)        public static event InformInfoHandler eveInfo;        //方法调用        public void DisplayInfo()        {            if (eveInfo != null)            {                eveInfo.Invoke("Test Message Content ...");            }        }    }}
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace A10_IndepthStudyDelegate{    class Registe    {        public Registe()        {            Invoke.eveInfo += InformZhangSan;            Invoke.eveInfo += InformLisi;            Invoke.eveInfo += InformWangWu;        }        public void InformZhangSan(string name)        {            Console.WriteLine("{0}Message for ZhangSan ...",name);        }        public void InformLisi(string name)        {            Console.WriteLine("{0}Message for Lisi ...", name);        }        public static void InformWangWu(string name)        {            Console.WriteLine("{0}Message for WangWu ...", name);        }    }}


using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace A10_IndepthStudyDelegate{    class Test    {        static void Main1(string[] args)        {            //运行事件的注册            new Registe();            //运行调用方            Invoke obj = new Invoke();            obj.DisplayInfo();        }    }}








原创粉丝点击