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

    将来的你,一定会感谢现在拼命努力的你!
  • 相关阅读:
    (转)导弹跟踪算法
    中文linux安装oracle界面乱码解决方案
    linux下创建oracle表空间
    [INS-20802] Oracle Net Configuration Assistant failed
    Centos6.5下Oracle 11g R2安装过程
    设置MYSQL数据库编码为UTF-8
    如何在linux系统中设置静态ip地址
    Oracle Net Configuration Assistant failed异常的解决方案
    解决安装oracle11g r2时提示pdksh conflicts with ksh-20100621-2.el6.i686问题
    CentOS增加swap分区大小
  • 原文地址:https://www.cnblogs.com/hugjil/p/6291400.html
Copyright © 2011-2022 走看看