zoukankan      html  css  js  c++  java
  • 小知识(七):代理&事件

    代理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的理解。如果有错别的地方麻烦您帮我指出来。万分感谢!

    将来的你,一定会感谢现在拼命努力的你!
  • 相关阅读:
    .NET 3.5新特性(转)
    (转)常用正则表达式
    IEC 61850(转)
    好几年了,我又回来了。
    EPR和SAP的一些名词解释(转载)
    为blogs添加风采,添加奥运金牌榜及赛程
    VS2010崩溃重启解决方法.
    C#制作Windows service服务系列二:演示一个定期执行的windows服务及调试(windows service)(转载)
    C#中操作XML (修改完整版) (转)
    C#制作Windows service服务系列一:制作一个可安装、可启动、可停止、可卸载的Windows service
  • 原文地址:https://www.cnblogs.com/hugjil/p/6291400.html
Copyright © 2011-2022 走看看