• 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()
            {
    
            }
    
    
        }
    }
  • 相关阅读:
    javascript高级知识点——函数原型
    javascript高级知识点——临时作用域
    javascript中的部分函数应用
    javascript高级知识点——闭包
    使用HIPQTools播放视频—记录
    wpa_suppliant编译小结
    内存溢出和内存泄漏
    C代码规范
    Makefiel(一)
    Lua语言
  • 原文地址:https://www.cnblogs.com/Mysterious/p/3415879.html
Copyright © 2020-2023  润新知