zoukankan      html  css  js  c++  java
  • 步步为营-27-事件

    1:先用委托演示

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    
    namespace Event_PlayMusic
    {
        //声明委托
        public delegate string delPlay();
        class PlayMusic
        {
            //定义委托
            public delPlay del;
            private string name;
    
            public string Name
            {
                get { return name; }
                set { name = value; }
            }
    
            public PlayMusic(string name)
            {
                this.name = name;
            }
    
            public void PlaySongs()
            {
                Thread.Sleep(3000);
                if (del != null)
                {
                    del();
                }
            }
    
        }
    }
    Form
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace Event_PlayMusic
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void btnPlaySong_Click(object sender, EventArgs e)
            {
                string songName = "小天狼";
                //事件--先触发后响应
                PlayMusic p = new PlayMusic(songName);
                MessageBox.Show("开始播放" + songName);
                p.del = () => { return "播放完毕"; };
                p.PlaySongs();
                MessageBox.Show(p.del());
             
            }
        }
    }
    Form

    效果
    将委托改为事件

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace Event_PlayMusic
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void btnPlaySong_Click(object sender, EventArgs e)
            {
                string songName = "小天狼";
                //事件--先触发后响应
                PlayMusic p = new PlayMusic(songName);
                MessageBox.Show("开始播放" + songName);
                p.del += () => { return "播放完毕"; };            
               string str = p.PlaySongs();
               MessageBox.Show(str);
             
            }
        }
    }
    Form1
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    
    namespace Event_PlayMusic
    {
        //声明委托
        public delegate string delPlay();
        class PlayMusic
        {
            //定义委托
            public event delPlay del;
            private string name;
    
            public string Name
            {
                get { return name; }
                set { name = value; }
            }
    
            public PlayMusic(string name)
            {
                this.name = name;
            }
    
            public string PlaySongs()
            {
                Thread.Sleep(3000);
                if (del != null)
                {
                   return  del();
                }
    
                return "事件为空!";
            }
    
        }
    }
    PlayMusic
  • 相关阅读:
    MyBatis动态SQL语句
    MyBatis分页
    理解 Linux 的处理器负载均值
    Linux命令之du
    Linux命令之df
    Linux命令之lsof
    maven打包加时间戳
    多线程学习-ListenableFuture使用介绍以及示例
    Host is not allowed to connect to this MySQL server解决方法
    Dapper,大规模分布式系统的跟踪系统
  • 原文地址:https://www.cnblogs.com/YK2012/p/6738723.html
Copyright © 2011-2022 走看看