小知识(七):代理&事件

代理delegate:

    对象引用    指向    某个特定类型的对象。

    代理        指向    某个特定类型的方法。

      

    代理四步:

  1. 定义自定义代理类:public delegate void first(int i);
  2. 实例化代理类:first MyDelegate = null;
  3. 实例添加方法:MyDelegate += new first(show);
  4. 通过实例对象调用方法:MyDelegate(666);

 

    class Program

    {

        //定义frist代理

         public delegate void first(int i);

        //主函数,main入口

        static void Main(string[] args)

        {

            //创建first类型引用

            first MyDelegate = null;

            //创建指向show方法的代理引用

            MyDelegate += new first(show);

            //通过代理引用调用show方法

            MyDelegate(666);

            Console.ReadKey();

        }

        //show方法

        public static void show(int i)

        {

            Console.WriteLine(i.ToString());

        }

    }

  1. 代理定义在方法外(包括类外)。
  2. 多重代理返回类型为void。
  3. 关键字delegate。
  4. 代理可以参数方式传到方法内部。

    如:

    class Program

    {

        //定义frist代理

         public delegate void first(int i);

        //主函数,main入口

        static void Main(string[] args)

        {

            //创建first类型引用

            first MyDelegate = null;

            //创建指向show方法的代理引用

            MyDelegate += new first(show);

            //通过代理引用调用show方法

            diao(666, MyDelegate);

            Console.ReadKey();

        }

        //show方法

        public static void show(int i)

        {

            Console.WriteLine(i.ToString());

        }

        //

public static void diao(int i,first dele)

{

            dele(i);

        }

    }

 

事件event:

//定义EventDelegate代理

    public delegate void EventDelegate();

    class Program

    {

        //主函数,main入口

        static void Main(string[] args)

        {

            //实例化ClockTimer

            ClockTimer clockTimer = new ClockTimer();

            //MyEvent中添加OnClockTimer方法

            clockTimer.MyEvent += new EventDelegate(OnClockTimer);

            //执行clickTimer对象的show方法

            clockTimer.show();

            Console.ReadLine();

        }

         //接受方法

        public static void OnClockTimer()

        {

            Console.WriteLine("收到时钟事件");

         }

    }

    //事件产生类

     public class ClockTimer

    {

        //定义事件(event)

        public event EventDelegate MyEvent;

        //产生事件方法

        public void show()

        {

            for(int i=0;i<1000;i++)

            {

            //产生事件

            MyEvent();

            //睡眠1秒

            Thread.Sleep(1000);//System.Threading;

            }

        }

    }

  1. 首先定义代理(类内或者类外定义)。

    用delegate关键字

  2. 定义触发事件(根据代理的作用域来定义)。假如代理定义在program类内部,则把触发事件写在program类内部。

    用event关键字。

  3. 定义处理触发事件的方法。

大师们,上面的是我对event和delegate的理解。如果有错别的地方麻烦您帮我指出来。万分感谢!

转载于:https://www.cnblogs.com/hugjil/p/6291400.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值