zoukankan      html  css  js  c++  java
  • 事件的学习例子

    1.利用委托实现事件例子

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication4
    {
        class Program
        {
            static void Main(string[] args)
            {
                MusicPlayer mp3 = new MusicPlayer();
                mp3.AfterStartedPlay = () =>
                {
                    Console.WriteLine("加载歌词!!!");
    
                    Console.WriteLine("加载动感背景");
                };
                mp3.StartPlay();//播放音乐
                mp3.BeforeMusicStop = ()=>{
                Console.WriteLine("删除歌词!!!!");
                    Console.WriteLine("停止动感背景!!!");
                
                  };
                mp3.EndMusic();//停止音乐
              
                
                //mp3.AfterStartedPlay();
                //mp3.BeforeMusicStop();//当把播放音乐mp3.StartPlay();和停止播放音乐mp3.EndMusic();注释,还是可以调用AfterStartedPlay();BeforeMusicStop();显然是不合理的。
                
                
                Console.WriteLine("ok");
                Console.ReadKey();
    
    
            }
    
    
    
        }
    
        class MusicPlayer {
            public Action AfterStartedPlay;//定义一个AfterStartedPlay委托
            public Action BeforeMusicStop;//定义一个BeforeMusicStop委托
            private void PlayMusic() {
            
                Console.WriteLine("开始播放音乐。。。。。");
            }
    
    
            public void StartPlay() {
                
             PlayMusic();
                if (AfterStartedPlay != null) {
    
                    AfterStartedPlay();//播放音乐后要做的事
                }
                Thread.Sleep(2000);
            
            }
            public void EndMusic() {
                if (BeforeMusicStop != null) {
    
                    BeforeMusicStop();//播放音乐完之前要做的事
                }
                Console.WriteLine("音乐播放完毕!");
            }
        
        }
    }

    2.使用事件实现

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication5
    {
        class Program
        {
            static void Main(string[] args)
            {
                MusicPlayer mp3 = new MusicPlayer();
                mp3.AfterStartedPlay += new Action(AfterStartedPlay);
               mp3.StartPlay();
                mp3.BeforeMusicStop += new Action(BeforeMusicStop); 
                //事件在外部不能直接调用
                //事件只能在定义事件的类的内部来触发
                //mp3.AfterStartedPlay();//报错
               // mp3.BeforeMusicStop();//报错
                mp3.EndMusic();
                Console.WriteLine("ok");
                Console.ReadKey();
    
    
            }
            static void AfterStartedPlay() {
    
                Console.WriteLine("加载歌词!!!");
    
                Console.WriteLine("加载动感背景");
            }
    
            static void BeforeMusicStop() {
                Console.WriteLine("删除歌词!!!!");
                Console.WriteLine("停止动感背景!!!");
            
            }
        }
    
        class MusicPlayer
        {
            public event Action AfterStartedPlay;//声明事件与声明委托变量特别像,就是在声明委托变量的前面加一个event关键字
            public event Action BeforeMusicStop;//当加了event关键字后,委托变量就变成了一个事件。
            private void PlayMusic()
            {
    
                Console.WriteLine("开始播放音乐。。。。。");
            }
    
    
            public void StartPlay()
            {
    
                PlayMusic();
                //事件的触发和调用的时候与委托变量的使用一模一样
                if (AfterStartedPlay != null)
                {
    
                    AfterStartedPlay();
                }
                Thread.Sleep(2000);
    
            }
            public void EndMusic()
            {
                if (BeforeMusicStop != null)
                {
    
                    BeforeMusicStop();
                }
                Console.WriteLine("音乐播放完毕!");
            }
    
        }
    }
  • 相关阅读:
    java 与打卡器通过udp协议交互
    java串口通信与打卡器交互
    hibernate 学习小结
    Log4J使用说明
    【秋招必备】Git常用命令(2021最新版)
    【秋招必备】Java集合面试题(2021最新版)
    工作这么多年!很多人竟然不知道线程池的创建方式有7种?
    【秋招必备】Java虚拟机面试题(2021最新版)
    【秋招必备】java异常面试题(2021最新版)
    好未来面试官:说说强引用、软引用、弱引用、幻象引用有什么区别?
  • 原文地址:https://www.cnblogs.com/Vinkong/p/9983990.html
Copyright © 2011-2022 走看看