• 通俗粗暴的事件委托理解


    简单粗暴的事件委托理解代码 

      1 using System;
      2 using System.Collections.Generic;
      3 
      4 public class MyClass
      5 {
      6     public static void RunSnippet()
      7     {
      8         Header head=new Header();
      9         XiaShuA xiashuaA=new XiaShuA(head);
     10         XiaShuB xiashuaB=new XiaShuB(head);
     11         head.Raise("");
     12         Console.WriteLine("================================");
     13         head.Raise("");
     14         Console.WriteLine("================================");
     15         head.Fall();
     16         Console.ReadLine();
     17     }
     18     
     19     #region Helper methods
     20     
     21     public static void Main()
     22     {
     23         try
     24         {
     25             RunSnippet();
     26         }
     27         catch (Exception e)
     28         {
     29             string error = string.Format("---
    The following error occurred while executing the snippet:
    {0}
    ---", e.ToString());
     30             Console.WriteLine(error);
     31         }
     32         finally
     33         {
     34             Console.Write("Press any key to continue...");
     35             Console.ReadKey();
     36         }
     37     }
     38 
     39     private static void WL(object text, params object[] args)
     40     {
     41         Console.WriteLine(text.ToString(), args);    
     42     }
     43     
     44     private static void RL()
     45     {
     46         Console.ReadLine();    
     47     }
     48     
     49     private static void Break() 
     50     {
     51         System.Diagnostics.Debugger.Break();
     52     }
     53 
     54     #endregion
     55 }
     56 
     57 
     58 public     delegate void RaiseEventHandler(string hand);
     59 public delegate void FallEventHandler();
     60 
     61 public class Header
     62 {
     63     public event RaiseEventHandler RaiseEvent;
     64     public event FallEventHandler FallEvent;
     65     
     66     public void Raise(string hand)
     67     {
     68         Console.WriteLine("首领 {0} 手举杯",hand);
     69         if(RaiseEvent!=null)
     70         {
     71             RaiseEvent(hand);
     72         }
     73     }
     74     
     75     public void Fall()
     76     {
     77         Console.WriteLine("首领摔杯");
     78         if(FallEvent !=null)
     79         {
     80             FallEvent();
     81         }
     82     }
     83 }
     84 
     85 public class XiaShuA
     86 {
     87     Header header;
     88     public XiaShuA(Header _head)
     89     {
     90         this.header=_head;
     91         this.header.RaiseEvent+=new RaiseEventHandler(FallRaise);
     92         this.header.FallEvent+=new FallEventHandler(Fallhand);
     93     }
     94     
     95     public void FallRaise(string hand)
     96     {
     97         if(hand=="")
     98         {
     99             Attach();    
    100         }
    101     }
    102     public void Fallhand()
    103     {
    104         Attach();
    105     }
    106     
    107     public void Attach()
    108     {
    109         Console.WriteLine("xiashuA立即攻击");
    110     }
    111 }
    112 
    113 public class XiaShuB
    114 {
    115     Header header;
    116     public XiaShuB(Header _head)
    117     {
    118         this.header=_head;
    119         this.header.RaiseEvent+=new RaiseEventHandler(FallRaise);
    120         this.header.FallEvent+=new FallEventHandler(Fallhand);
    121     }
    122     
    123     public void FallRaise(string hand)
    124     {
    125         if(hand=="")
    126         {
    127             Attach();    
    128         }
    129     }
    130     public void Fallhand()
    131     {
    132         Attach();
    133     }
    134     
    135     public void Attach()
    136     {
    137         Console.WriteLine("xiashubB立即攻击");
    138     }
    139 }
  • 相关阅读:
    K3s+Jetson Nano,在边缘端实现实时视频分析!
    15分钟连接Jetson Nano与K8S,轻松搭建机器学习集群
    配置高可用K3s集群完全攻略
    K3s+Sysdig,8分钟部署并保护集群安全!
    1款工具助力Rancher HA快速部署,极速提升研发测试效率
    连刷40道题,告别动态规划,谈谈我的经验
    直通BAT算法精讲视频教程分享
    关于三次握手和四次挥手,面试官想听到怎样的回答?
    Redisson 分布式锁实战与 watch dog 机制解读
    Spring 注解动态数据源设计实践
  • 原文地址:https://www.cnblogs.com/mikechang/p/5058643.html
Copyright © 2020-2023  润新知