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

    代码背景:

    当你取钱时,把取钱的短信发到手机同时把信息发到邮箱

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace Delegatevent
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("请输入取钱金额:");
                int x = Convert.ToInt32(Console.ReadLine());
                //实例化对象类赋值
                Duixiang d = new Duixiang("521@qq.com", "15420000112", x);
                //实例化委托类
                Brank b = new Brank();
                //调用事件触发后的方法
                b.Quq += new Brank.QuqEventHender(Pfashong.Duanxin);
                b.Quq += new Brank.QuqEventHender(PEmail.Duanxin);                     
                //调用触发事件
                b.GetEvent(d);
                Console.ReadLine();

            }
        }
        public class Pfashong
        {
            //手机短信发送
            public static void Duanxin(object s, Duixiang e)
            {
                Console.WriteLine("手机号:"+e.Pone+"在"+DateTime.Now.ToString()+"取了"+e.Mone+"元");
            }
        }
        public class PEmail
        {
            //手机短信发送
            public static void Duanxin(object s, Duixiang e)
            {
                Console.WriteLine("邮箱:" + e.Emial + "在" + DateTime.Now.ToString() + "取了" + e.Mone + "元");
            }
        }
        //信息发布
        public class Brank
        {
            //定义一个委托
            public delegate void QuqEventHender(object sender, Duixiang e);
            //定义事件
            public event QuqEventHender Quq;
            //事件方法
            public void GetEvent(Duixiang e)
            {
                //判断事件是否有人注册
                if (Quq != null)
                {
                    Quq(this, e);
                }
            }
        }


        public class Duixiang:EventArgs
        {
            //邮箱字段
            public readonly string Emial;
            //手机号
            public readonly string Pone;
            //取钱字段
            public readonly int Mone;

            //构造函数
            public  Duixiang(string Emial, string Pone, int Mone)
            {
                this.Emial = Emial;
                this.Pone = Pone;
                this.Mone = Mone;
            }
        }
    }

  • 相关阅读:
    elasticsearch客户端连接选择
    logstash5.x配置
    git操作
    Linux的crontab
    让MySQL支持中文
    Python装饰器(decorator)
    Python类的探讨
    Python对象(译)
    Python基础-作用域和命名空间(Scope and Namespace)
    Python输入输出(IO)
  • 原文地址:https://www.cnblogs.com/lhn5xy/p/8379357.html
Copyright © 2011-2022 走看看