zoukankan      html  css  js  c++  java
  • 事件

    在 某件事情发生时,一个对象可以通过事件通知另一个对象。比如,前台完成了前台界面,他通知你,可以把前台和你开发的程序整合了。这就是一个事件。可以看出 事件是在一个时间节点去触发另外一件事情,而另外一件事情怎么去做,他不会关心。就事件来说,关键点就是什么时候,让谁去做。

    在C#中,时间定义关键字是event。例如:
    event ProcessDelegate ProcessEvent;

    整个事件定义方法以及执行过程:

    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace TestApp
    {
        /// <summary>
        /// 委托
        /// </summary>
        /// <param name="s1"></param>
        /// <param name="s2"></param>
        /// <returns></returns>
        public delegate void ProcessDelegate(object sender, EventArgs e);

        class Program
        {
            

            static void Main(string[] args)
            {
                /*  第一步执行  */
                Test t = new Test();
                /* 关联事件方法,相当于寻找到了委托人 */
                t.ProcessEvent += new ProcessDelegate(t_ProcessEvent);
                /* 进入Process方法 */
                Console.WriteLine(t.Process()); 

                Console.Read();
            }

            static void t_ProcessEvent(object sender, EventArgs e)
            {
                Test t = (Test)sender;
                t.Text1 = "Hello";
                t.Text2 = "World";
            }
        }

        public class Test
        {
            private string s1;

            public string Text1
            {
                get { return s1; }
                set { s1 = value; }
            }

            private string s2;

            public string Text2
            {
                get { return s2; }
                set { s2 = value; }
            }


            public event ProcessDelegate ProcessEvent;

            void ProcessAction(object sender, EventArgs e)
            {
                if (ProcessEvent == null)
                    ProcessEvent += new ProcessDelegate(t_ProcessEvent);
                ProcessEvent(sender, e);
            }

            //如果没有自己指定关联方法,将会调用该方法抛出错误
            void t_ProcessEvent(object sender, EventArgs e)
            {
                throw new Exception("The method or operation is not implemented.");
            }

            void OnProcess()
            {
                ProcessAction(this, EventArgs.Empty);
            }

            public string Process()
            {
                OnProcess();
                return s1 + s2;
            }
        }
    }
     

    感觉到了什么?是不是和代码注入了差不多,相当于是可以用任意符合委托接口(委托确实很像接口)的代码,注入到Process过程。在他返回之前给他赋值。

  • 相关阅读:
    Some projects cannot be imported because they already exist in the workspace 解决方法!!!
    Python XLRDError: Excel xlsx file; not supported解决方法
    多人VNC远程桌面服务配置
    手把手教你配置KVM服务器
    Python实现加密压缩成RAR或ZIP文件
    Python实现加密的ZIP文件解压(密码已知)
    Python实现加密的RAR文件解压(密码已知)
    Office:应用程序无法正常启动0xc0000142
    引入transformers 报错 Segmentation fault (core dumped)
    Asymmetrical Hierarchical Networks with Attentive Interactions for Interpretable Review-Based Recommendation
  • 原文地址:https://www.cnblogs.com/lyl6796910/p/3803050.html
Copyright © 2011-2022 走看看