• c#之委托和事件


    一:委托简介
    委托是一种指针,保持方法的引用
    委托使得方法可以作为参数进行传递
    如:

        public delegate void MyDelegate();
        class Program
        {
            private static void SayHellow()
            {
                Console.WriteLine("Hellow world");
            }
            static void Main(string[] args)
            {
                //MyDelegate a = new MyDelegate(SayHellow);//也可以
                MyDelegate a = SayHellow;
                a();
                Console.ReadKey();
            }
        }

    委托其实是一种特殊的类
    他定义了方法的签名
    使得与该签名相同的方法都能被此委托所引用
    有了这个特性就可以把方法当作参数进行传递

    二:匿名方法
    委托可以不用已存在的方法创建

        public delegate void MyDelegate();
        class Program
        {
            static void Main(string[] args)
            {
                MyDelegate a = delegate()
                {
                    Console.WriteLine("HellowWorld");
                };
                a();
                Console.ReadKey();
            }
        }

    匿名方法在lambda表达式出来之后,更见锋芒,以后再说
    委托可以绑定匿名方法,实例方法和静态方法

    三:多播委托
    委托支持操作符重载
    可以将多个方法绑定到同一个委托
    也可以从一个委托移除某一方法
    可以把一个方法绑定多次,运行时也会执行多次

        public delegate void MyDelegate();
        class Program
        {
            private static void SayHellow()
            {
                Console.WriteLine("Hellow world");
            }
            private static void SayHellow2()
            {
                Console.WriteLine("Hey world");
            }
            static void Main(string[] args)
            {
                MyDelegate a = SayHellow;
                a += SayHellow2;
                a();
                Console.ReadKey();
            }
        }

    一个委托实例指向多个方法,这些方法是无序的,设计时不要依赖这种顺序

    四:事件
    可以不用初始化事件就直接用+=操作符绑定方法
    观察者模型(此方法JimmyZiYang原创,此处做了适当修改,在此表示感谢)

     public delegate void BoiledEventHandler(object sender,BoliedEventArgs e);
        
        public class BoliedEventArgs : EventArgs
        {
            public readonly int temperature;
            public BoliedEventArgs(int temperature)
            {
                this.temperature = temperature;
            }
        }
    
        public class Heater 
        {
            private int temprature;
            public string type = "RealFire 001";
            public string area = "HangZhou China";
            public event BoiledEventHandler Boiled;
            protected virtual void OnBolied(BoliedEventArgs e)
            {
                if (Boiled != null)
                { Boiled(this, e); }
            }
            public void BoilWater()
            {
                for (int i = 0; i < 100; i++)
                {
                    temprature = i;
                    if (temprature > 95)
                    {
                        BoliedEventArgs e = new BoliedEventArgs(temprature);
                        OnBolied(e);
                    }
                }
            }
        }
        public class Alarm
        {
            public void MakeAlert(object Sender, BoliedEventArgs e)
            {
                Heater heater = (Heater)Sender;
                Console.WriteLine("Alarm:{0}-{1}", heater.area, heater.type);
                Console.WriteLine("Alarm:水已经到{0}度了", e.temperature);
                Console.WriteLine();
            }
        }
        public class Display
        {
            public static void ShowMsg(object sender, BoliedEventArgs e)
            {
                Heater heater = (Heater)sender;
                Console.WriteLine("display:{0}-{1}", heater.area, heater.type);
                Console.WriteLine("display:水快烧开了,当前温度{0}", e.temperature);
                Console.WriteLine();
            }
        }
        class Program
        {
            
            static void Main(string[] args)
            {
                Heater heater = new Heater();
                Alarm alarm = new Alarm();
                heater.Boiled += alarm.MakeAlert;
                heater.Boiled += Display.ShowMsg;
    
                heater.BoilWater();
                Console.ReadKey();
            }
        }

    输出结果
    image

    本文编写过程中得到了  钧梓昊逑  的帮助,在此表示感谢!

  • 相关阅读:
    jenkins 安装及配置(Windows环境)
    .net 部署iis 为https安全协议解决方案
    利用Aspose转换office成PDF API程序
    Quickuse.Lock 快速应用.锁
    微信公众号获取图片时报错43003
    C# Windows系统运行exe程序提示'net'不是内部或外部命令,也不是可运行的程序或批处理文件
    C# 共享目录如何登录
    Window系统 如何拷贝命令提示符中的数据
    Windows系统 如何获取文件的MD5
    jq table 自动滚动
  • 原文地址:https://www.cnblogs.com/liulun/p/1491912.html
Copyright © 2020-2023  润新知