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

    最近学习了委托和事件,在网上也找过很多资料,发现很多都是讲的差不多,对初学者来说,很难理解的,自己从网上找了不少列子,总结一下,希望对大家有所帮助

    对于概念的介绍,网上很多,大家可以自己去看看,主要是自己也不怎么清楚,这里就给大家直接上手几个例子,这样会比较直观

    1.委托

      两个窗体之间的传值,我想这个大家肯定都会遇到,也是一个比较好的案例吧

      form1窗体有个按钮,点击弹出form2窗体,form2窗体上有一个text文本框,点击form2上的按钮,将文本框中的值输出到form1窗体上(用lable接收)

      图片:

      

      

      from1中的代码:

      

    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 委托
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                Form2 f2 = new Form2(aa);
                f2.Show();
            }
            public void aa(string name) 
            {
                label1.Text = name;
            }
        }
    }

      from2中的代码:

      

    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 委托
    {
    
        //声明一个委托  委托的返回值,和参数必须和要传递的函数一样
      //这里要传递的函数就是form1中的aa函数
    //委托就是把函数当做参数进行传递 public delegate void Delaa(string name); public partial class Form2 : Form { //声明一个字段,用来存贮传递过来的方法 public Delaa _del; public Form2(Delaa ab) { this._del = ab; InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { _del(textBox1.Text); } } }

      总结一下:

       1、委托实际上就是指针,就是方法的地址,程序中你让它指向哪个方法它就指向哪个方法;
        2、委托是统一的方法模型,参数必须一致;
        3、委托实际上是把方法当作参数来传递,可以是静态方法也可以是非静态的方法。

    2.事件:

      个人感觉要用事件,必须要用到委托,

      废话不多说,直接上代码:写例子,

      列子:定义3个类,一个是将军类,士兵a类,士兵b类

        将军举左手的时候,士兵a攻击,右手的时候,士兵b攻击,摔杯a,b同时攻击

      将军类:

      

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace 委托2
    {
    
        //声明2个委托
        //委托的命名规则:命名规则是在准备传递的方法名后加上EventHandler,也可以不这样用,只不过大家都这么用
        //第一个委托是有参数的,第二个委托无参数
        public delegate void RaiseEventHandler(string str);
        public delegate void FallEventHandler();
    
        class Leader
        {
            //将创建的委托与特定的事件关联
            public event RaiseEventHandler RaiseEvent;
            public event FallEventHandler FallEvent;
    
            public void Raise(string str) 
            {
                Console.WriteLine("首领"+str+"举杯",str);
                if (RaiseEvent != null) 
                {
                    RaiseEvent(str);
                }
            
            }
    
            public void Fall() 
            {
                Console.WriteLine("首领摔杯");
                if (FallEvent != null) 
                {
                    FallEvent();
                }
            }
    
        }
    }

      士兵A:

      

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace 委托2
    {
        class SolidA
        {
            //创建将军
            Leader Ld;
    
            //在构造函数中传递将军类
            public SolidA(Leader ld) 
            {
                //赋值
                this.Ld = ld;
                //创建2个事件对象,2个事件都指定到对应的方法中
                Ld.RaiseEvent += new RaiseEventHandler(S_Att);
                Ld.FallEvent += new FallEventHandler(SumAtt);
            }
    
            
            public void S_Att(string str) 
            {
                if (str == "左手") 
                {
                    attack();
                }
            }
    
            public void SumAtt() 
            {
                attack();
            }
    
            public void attack()
            {
                Console.WriteLine("士兵A发动攻击");
                Console.ReadLine();
            }
        }
    }

     士兵B:

      

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace 委托2
    {
        class SolidB
        {
    
            //创建将军
            Leader Ld;
            //在构造函数中传递将军类
            public SolidB(Leader ld) 
            {
                //赋值
                this.Ld = ld;
                //创建2个事件对象,2个事件都指定到对应的方法中
                ld.RaiseEvent += new RaiseEventHandler(B_Att);
                ld.FallEvent += new FallEventHandler(Sum_Att);
            }
    
            public void attack() 
            {
                Console.WriteLine("士兵B发动攻击");
                Console.ReadLine();
            }
    
            public void B_Att(string str) 
            {
                if (str == "右手") 
                {
                    attack();
                }
            }
    
            public void Sum_Att()
            {
                attack();
            }
        }
    
    }

      在main函数中调用:

      

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace 委托2
    {
        class Program
        {
            static void Main(string[] args)
            {
    
                Leader ld = new Leader();
                SolidA sa = new SolidA(ld);
                SolidB sb = new SolidB(ld);
    
                ld.Raise("左手");
                Console.ReadLine();
    
    
            }
        }
    }

      希望对大家能够有所帮助,不足之处,欢迎大家留言!

  • 相关阅读:
    IDEA成功注册方法
    H2O中的随机森林算法介绍及其项目实战(python实现)
    scala语言简介及其环境安装
    SparkSQL---实战应用
    Spark算子---实战应用
    利用python的KMeans和PCA包实现聚类算法
    Spark MLlib回归算法------线性回归、逻辑回归、SVM和ALS
    m个苹果放在n个盘子里面有多少种放法?(动态规划)
    Java_CookieUtil
    Jquery_AjaxFileUpload插件的使用记录
  • 原文地址:https://www.cnblogs.com/qingnianxu/p/7218574.html
Copyright © 2011-2022 走看看