zoukankan      html  css  js  c++  java
  • 委托和事件

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace DelegateAndEvent
    {
       public  class Caculator
        {
            //dim a CaculateEventArgs,used to save the status info when call the event
            public class CaculateEventArgs : EventArgs
            {
                public readonly int x, y;
                public CaculateEventArgs(int x, int y)
                {
                    this.x = x;
                    this.y = y;
                }
            }
            //dim the event delegate
            public delegate void CalculateEventHandler(object sender,CaculateEventArgs e);
            //dim the event
            public event CalculateEventHandler MyCalculate;
            //support the virtual function
           //提供受保护的虚方法,可以由子类覆写来拒绝监视
            protected virtual void OnCalculate(CaculateEventArgs e)
            {
                if(MyCalculate!=null)
                {
                    MyCalculate(this, e);
                }
            }
            //进行计算,调用该发放表示有新的计算发生
            public void Calculate(int x,int y)
            {
                CaculateEventArgs e = new CaculateEventArgs(x,y );
                //通知所有的事件注册者
                OnCalculate(e);
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace DelegateAndEvent
    {
        /// <summary>
        /// 丁原事件触发者
        /// </summary>
       public  class CaculatorManager
        {
           public void Add(object sender,Caculator.CaculateEventArgs e)
           {
               Console.WriteLine(e.x+e.y );
           }
           public void Substract(object sender,Caculator.CaculateEventArgs e)
           {
               Console.WriteLine(e.x-e.y);
           }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace DelegateAndEvent
    {
        class Program
        {
            static void Main(string[] args)
            {
                Caculator calculator = new Caculator();
                //事件触发者
                CaculatorManager cm = new CaculatorManager();
                //事件绑定
                calculator.MyCalculate += cm.Add;
                calculator.Calculate(100,200);
                calculator.MyCalculate += cm.Substract;
                calculator.Calculate(100,200);
    
                //事件注销
                calculator.MyCalculate -= cm.Add;
                calculator.Calculate(100,200);
                Console.Read();
            }
        }
    }

    输出:

    300

    300

    -100

    -100

  • 相关阅读:
    ASP.NET MVC5 :Attribute路由使用详解
    C# 常用字符串加密解密方法
    C#获取当前主机硬件信息
    用C#调用Windows API向指定窗口发送按键消息
    Win32 编程消息常量(C#)
    C#程序员开发WinForm必须知道的 Window 消息大全
    C#通过SendMessage发送消息,改变其他程序的下拉框控件(ComboBox)的值
    C#常用 API函数大全
    SendKeys发送组合键
    webapi get请求 FromUri list参数传递
  • 原文地址:https://www.cnblogs.com/tylertang/p/4210977.html
Copyright © 2011-2022 走看看