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

    将来的你,一定会感谢现在拼命努力的你!
  • 相关阅读:
    SQL SELECT INTO 语句
    跨站脚本攻击测试[转]
    Visual Studio 2008中如何比较二个数据库的架构【Schema】和数据【Data】并同步 [转贴]
    联表UPDATE
    网站架构策划方案
    概念模型 逻辑模型 物理模型 区别
    Repeater嵌套绑定Repeater
    .net 2.0升级到了.net 3.5,开发工具从vs2005转为vs2008
    你必须知道的C#的25个基础概念(附演示) 【转】
    window2003中,在IIS中,如何解决不能播放.FLV文件
  • 原文地址:https://www.cnblogs.com/hugjil/p/6291400.html
Copyright © 2011-2022 走看看