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

    定义委托语法:

    访问修饰符delegate 返回值类型 委托名称(参数1,参数2,参数n …)

    定义事件的语法:

    public event 委托名 事件名;

    控制台应用程序实例:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Event
    {
        class Program
        {
            static void Main(string[] args)
            {
                Employee e1 = new Employee("小白",988);
                Reporter re = new Reporter();
    
                //指明玩游戏这个事件触发后,由谁来处理
                e1.PlayGame += new DelegateClassHandle(re.Notify);
    
                e1.Game();
            }
    
        }
    
        public class Employee
        {
            /// <summary>
            /// DelegateClassHandle的事件
            /// </summary>
            public event DelegateClassHandle PlayGame;
    
            private string _name;
    
            public string Name
            {
                get { return _name; }
                set { _name = value; }
            }
            private int _number;
    
            public int Number
            {
                get { return _number; }
                set { _number = value; }
            }
    
            public Employee(string n,int nu)
            {
                _name = n;
                _number = nu;
            }
    
            public void Game()
            {
                Console.WriteLine("我要玩游戏喽!");
    
                CustomEventnArgs e = new CustomEventnArgs();
                e.Name = _name;
                e.Number = _number;
                if (PlayGame != null)
                {
                    PlayGame(this,e);
                }
            }
        }
        
        /// <summary>
        /// 委托
        /// </summary>
        /// <param name="sender">触发事件的源</param>
        /// <param name="e">同时携带的参数</param>
        public delegate void DelegateClassHandle(object sender,CustomEventnArgs e);
    
        /// <summary>
        /// 具体报告
        /// </summary>
        public class Reporter
        {
            public void Notify(object sender,CustomEventnArgs e)
            {
                Console.WriteLine("报告老板,{0}在{1}办公室玩游戏!",e.Name,((Employee)sender).Number);
                //或者
                //Console.WriteLine("报告老板,{0}在{1}玩游戏!", e.Name, e.Number);
            }
        }
    
        public class CustomEventnArgs : EventArgs
        {
            string name;
    
            public string Name
            {
                get { return name; }
                set { name = value; }
            }
            int number;
    
            public int Number
            {
                get { return number; }
                set { number = value; }
            }
    
            public CustomEventnArgs()
            {
    
            }
    
    
        }
    }
  • 相关阅读:
    js中取整数的方法
    js中原型和原型链
    js中获取class封装
    Cocos2dx打包成apk包时在手机上闪退
    计算两个日期间相差的天数
    Stealing a Cake [函数方程符合凸性 三分]
    插件式架构设计(转)
    转:Entity FrameWork利用Database.SqlQuery<T>执行存储过程并返回参数
    后台web请求代码(含https,json提交)
    sitecore 缓存管理器
  • 原文地址:https://www.cnblogs.com/Mysterious/p/3415879.html
Copyright © 2011-2022 走看看