zoukankan      html  css  js  c++  java
  • Net中事件的高级用法之三

    Net中事件的高级用法之三

    1.事件的高级应用

    使用事件可以解除代码耦合

    2.事件高级应用实例

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace MyEvent
    {
        public delegate void MiaoEventDelegate();
        public class Cat
        {
            public event MiaoEventDelegate MiaoEventHandler;
            public void Miao()
            {
                Console.WriteLine("猫,苗一声");
                Mouse.Run();
                Dog.Wang();
                Neighbor.Awake();
                Stealer.Hide();
                Baby.Dry();
            }
            public void MiaoEvent(){
                Console.WriteLine("**************MiaoEvent*****************");
                Console.WriteLine("另外一只猫,喵一声");
                if (MiaoEventHandler !=null)
                {
                    MiaoEventHandler();
                }
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace MyEvent
    {
        public class Baby
        {
            public static void Dry()
            {
                Console.WriteLine("小孩子哭");
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace MyEvent
    {
        public class Dog
        {
            public static void Wang()
            {
                Console.WriteLine("狗叫");
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace MyEvent
    {
        public class Mouse
        {
            public static void Run()
            {
                Console.WriteLine("老鼠跑");
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace MyEvent
    {
        public class Neighbor
        {
            public static void Awake()
            {
                Console.WriteLine("邻居被吵醒");
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace MyEvent
    {
        public class Stealer
        {
            public static void Hide(){
                Console.WriteLine("小偷藏起来");
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace MyEvent
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("欢迎来的流星小子博客学习");
                Cat cat = new Cat();
               // cat.Miao();
                cat.MiaoEventHandler += Mouse.Run;
                cat.MiaoEventHandler += Dog.Wang;
                cat.MiaoEventHandler += Neighbor.Awake;
                cat.MiaoEventHandler += Stealer.Hide;
                cat.MiaoEventHandler += Baby.Dry;
                cat.MiaoEvent();
    
                Console.Read();
            }
        }
    }
  • 相关阅读:
    uboot的配置文件在哪里
    uboot的flash sectore的大小由哪个宏指定
    openwrt中的append-ubi定义在哪里
    vi在行首插入注释符号#
    openwrt设置uboot环境变量在flash上的存储地址
    git重命名分支名
    ifconfig设置ip时出现提示 ifconfig: SIOCSIFFLAGS: Address not available
    jquery checkbox选中、改变状态、change和click事件
    jQuery操作复选框checkbox技巧总结 ---- 设置选中、取消选中、获取被选中的值、判断是否选中等
    checkbox选择根据后台List数据进行回显
  • 原文地址:https://www.cnblogs.com/dongh/p/14768333.html
Copyright © 2011-2022 走看看