马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

委托与事件是什么关系

[复制链接]
查看3865 | 回复0 | 2013-12-28 16:26:07 | 显示全部楼层 |阅读模式
从博客园看到的 转载来。

[C#] 纯文本查看 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace eventDelegate
{
    //通水人
    class middleBoy
    {
        //
     public   delegate void playGame(object obj, playGameEventArgs e);
    }


    //消息
    class playGameEventArgs:EventArgs
    {
        public string name{get;set;}

    }
    class Boss
    {
        public static void notify(object obj, playGameEventArgs e)
        {
            Console.WriteLine(e.name + " 玩游戏了");
        }
    }
    //管理人2
    class Admin
    {
        public static void notify(object obj, playGameEventArgs e)
        {
            Console.WriteLine(e.name + " 玩游戏了");
        }
    }
    class employee
    {

        private string name{get;set;}
        public employee(string n)
        {
            name = n;
        }

        public event middleBoy.playGame game;
        public void iPlayGame()
        {
            if (game != null)
            {
                playGameEventArgs e=new playGameEventArgs();
                e.name = name;
                game(this,e);
            }
        }
    }


    class Program
    {
        static void Main(string[] args)
        {
            employee ee = new employee("Javk");
            ee.game += new middleBoy.playGame(Boss.notify);
            ee.game += new middleBoy.playGame(Admin.notify);
            ee.iPlayGame();
            Console.ReadLine();
        }
    }
}

1.delegate做 监控人、事件发生者 之间的通讯人
2. 新的事件发生者的参数需要继承EventArgs,做成新的消息
3.事件发生者要判断事件是否为空再运行行动
4.多路发布遵守OPP原则(原基类封闭原则,开放业务类)
[C#] 纯文本查看 复制代码
//消息
    class playGameEventArgs:EventArgs
    {
        public string name{get;set;}

    }
    class Boss
    {
        public static void notify(object obj, playGameEventArgs e)
        {
            Console.WriteLine(e.name + " 玩游戏了");
        }
    }
    //管理人2
    class Admin
    {
        public static void notify(object obj, playGameEventArgs e)
        {
            Console.WriteLine(e.name + " 玩游戏了");
        }
    }
    class employee
    {

        private string name{get;set;}
        public employee(string n)
        {
            name = n;
        }
//用泛式代替了代理
        public event EventHandler<playGameEventArgs> game;
        public void iPlayGame()
        {
            if (game != null)
            {
                playGameEventArgs e=new playGameEventArgs();
                e.name = name;
                game(this,e);
            }
        }
    }


    class Program
    {
        static void Main(string[] args)
        {
            employee ee = new employee("Javk");
            ee.game += Boss.notify;
            ee.game +=Admin.notify;
            ee.iPlayGame();
            Console.ReadLine();
        }
    }
}



C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则