委托事件

代码背景:

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

 

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;
        }
    }
}

posted @ 2018-01-29 20:24  萌髦  阅读(93)  评论(0编辑  收藏  举报