• 【基础知识】委托与事件


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            /// <summary>
            /// 鼠标左键点击触发事件
            /// </summary>
            /// <param name="sender">指发送通知的对象</param>
            /// <param name="e">包含所有通知接受者包含的附件的信息</param>
            private void button1_Click(object sender, EventArgs e)
            { 
                Cat cat = new Cat("Tom");
                Mouse mouse1 = new Mouse("Jack");
                Mouse mouse2 = new Mouse("Jerry");
                cat.CatShout+=new Cat.CatShoutEventHandler(mouse1.Run);//实例化一个委托,而委托的实例其实就是Mouse的Run方法
                cat.CatShout += new Cat.CatShoutEventHandler(mouse2.Run);
                cat.Shout();//只有当猫叫时才会触发老鼠动作
            }
        }
        //委托是对函数的封装,可以当做给方法的特征指定一个名称。  事件是委托的特殊形式,当发生有意义的事情时,事件对对象处理通知
        /// <summary>
        ////// </summary>
        class Cat
        {
            private string name = "";
            public Cat(string name)
            {
                this.name = name;
            }
            //public delegate void CatShoutEventHandler();//委托
            //public event CatShoutEventHandler CatShout;//事件
            public delegate void CatShoutEventHandler(object sender,CatShoutEventArgs args);//委托
            public event CatShoutEventHandler CatShout;//事件
            public void Shout()
            {
                MessageBox.Show("我是只大花猫,我的名字叫做:"+name);
                if (CatShout!=null)
                {
                    CatShoutEventArgs e = new CatShoutEventArgs();
                    e.Name = this.name;
                    CatShout(this,e);
                }
            }
        }
        /// <summary>
        ////// </summary>
        class Mouse
        {
            private string name="";
            public  Mouse(string name)
            {
                this.name=name;
            }
            //public void Run()
            //{
            //    MessageBox.Show("我是只大老鼠,我的名字叫:"+name);
            //}
            public void Run(object sender,CatShoutEventArgs args)
            {
                string message=string.Format("大花猫{0}来了,小老鼠{1}快快跑啊!" ,args.Name,name);
                MessageBox.Show(message);
            }
        }
        /// <summary>
        /// //继承包含事件数据的类的基类
        /// </summary>
        public class CatShoutEventArgs : EventArgs
        {
            private string name = "";
            public String Name
            {
                get { return name; }
                set { name = value; }
            }
        }
    }
  • 相关阅读:
    多网卡绑定
    deepin 20.2.3 数字时钟屏保
    (原创)odoo15(master)下,列表导出权限控制
    在CentOS7上扩容centos-root根目录
    Linux扩容-新增磁盘分区挂载-fdisk
    Docker
    隐私政策(URL)
    快排代码
    反射将对象所有属性(含集合中所有属性)中字符串类型做trim()
    Prometheus 查询语句
  • 原文地址:https://www.cnblogs.com/songxxu/p/3550273.html
Copyright © 2020-2023  润新知