• 委托和事件


    1.委托

      1)委托类型是用户自定义类型   比作MyClass类型

      2)委托存储的就是一系列具有相同签名和返回回类型的方法的地址  比作MyClass 类型 的变量存储的是对象的地址。

      问题1:为什么使用委托?

      回复:每种交通工具 的发动机都不一样,你不可能为每个交通工具的构造函数设置各种不同的发动机类,但是如果我们

      用委托作为入参,指向发动的方法,那么客户端在调用时只关注发动的方法,不必关注哪种发动机。

        public class Program
        {
            //定义委托类型
            public delegate void DPrint(string msg);
            static void Main(string[] args)
            {
                DPrint Print;//声明一个委托
                Print = Program.Print;//赋值委托 指定一个对象的函数的指针
                Print += Program.PrintLine;//组合委托
                Print("msg");//委托调用
    
                DPrint myPrint = delegate(string msg)//委托指向匿名方法
                {
                    Console.WriteLine("myPrint:"+msg);
                };
                myPrint("msg1");
    
                DPrint lambdaPrint = (string msg) =>//Lambda表达式:简化匿名方法
                {
                    Console.WriteLine("lambdaPrint:" + msg);
                };
                lambdaPrint("msg2");
                Console.ReadLine();
            }
            public static void Print(string msg)
            {
                Console.WriteLine("Print:" + msg);
            }
            public static void PrintLine(string msg)
            {
                Console.WriteLine("PrintLine:" + msg);
            }
        }
    View Code

       使用委托前的代码:

        public class Car
        {
            public Car(CarMove dMove)
            {
             
            }
        }
    
        public class Bicycle
        {
            public Bicycle(BicycleMove dMove)
            {
               
            }
        }
        public class BicycleMove
        {
            public BicycleMove()
            {
                Console.WriteLine("BicycleMove");
            }
        }
        public class CarMove
        {
            public CarMove()
            {
                Console.WriteLine("CarMove");
            }
        }
        public class Program
        {
    
            static void Main(string[] args)
            {
                new Car(new CarMove());//new Car(new BicycleMove());
                Console.ReadLine();
            }
    
        }
    View Code

          使用委托后的代码:

        public delegate void DMove();
        public class Car
        {
            public Car(DMove dMove)
            {
                dMove();
            }
        }
    
        public class Bicycle
        {
            public Bicycle(DMove dMove)
            {
                dMove();
            }
        }
        public class Program
        {
    
            static void Main(string[] args)
            {
                new Car(Program.BicycleMove);
                Console.ReadLine();
            }
    
            public static void CarMove()
            {
                Console.WriteLine("CarMove");
            }
            public static void BicycleMove()
            {
                Console.WriteLine("BicycleMove");
            }
        }
    View Code

    2.事件  

        /// <summary>
        /// 事件发送方和接收的数据信使
        /// </summary>
        public class Extra : EventArgs
        {
            public string Note { get; set; }
        }
    
        /// <summary>
        /// 事件发送方
        /// </summary>
        public class Reader
        {
            public delegate void LookDelegate(object sender, Extra eventArgs);
            public event LookDelegate LookEvent;
            public void Read(Extra bookEventArgs)
            {
                LookEvent(this, bookEventArgs);
            }
        }
    
        /// <summary>
        /// 事件接收方
        /// </summary>
        public class Writer
        {
            public Writer(Reader reader)
            {
                reader.LookEvent += new Reader.LookDelegate(this.Write);
            }
            public void Write(object sender, Extra bookEventArgs)
            {
                Console.Write("Write:" + bookEventArgs.Note);
            }
        }
    
        public class LineWriter
        {
            public LineWriter(Reader reader)
            {
                reader.LookEvent += new Reader.LookDelegate(this.WriteLine);
            }
            public void WriteLine(object sender, Extra bookEventArgs)
            {
                Console.WriteLine("WriteLine:" + bookEventArgs.Note);
            }
        }
    View Code
                Extra book = new Extra { Note = "aaa" };
                Reader reader = new Reader();
                LineWriter LineWriter = new LineWriter(reader);
                Writer writer = new Writer(reader);           
                reader.Read(book);
                Console.ReadLine();
    客户端调用
  • 相关阅读:
    MySQL 查询树结构、循环查询、查看函数、视图、存储过程
    SpringBoot 部署:外置依赖包
    springboot集成docker实现自动化构建镜像说明
    Chrome谷歌浏览器常用快捷键、开发技巧
    Windows常用CMD命令
    Git 版本管理,与 SVN区别对比
    TensorFlow重新导入restore报错: OP_REQUIRES failed at save_restore_v2_ops.cc:184 : Not found: Key Variable not found in checkpoint
    MySQL字符集设置
    Hive 时间操作
    深度思考:如何规划你的职业生涯
  • 原文地址:https://www.cnblogs.com/liandy0906/p/7389289.html
Copyright © 2020-2023  润新知