zoukankan      html  css  js  c++  java
  • 委托和事件

    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();
    客户端调用
  • 相关阅读:
    第一阶段冲刺4
    用户场景分析
    最小不重复数
    BOM
    虚拟机下ubuntu系统设置分辨率
    富文本编辑器KindEditor使用
    页面路径设置
    VMware虚拟机不能上网的问题
    Apache Tomcat/7.0.42配置用户
    JFreeChart 横轴文字竖着显示
  • 原文地址:https://www.cnblogs.com/liandy0906/p/7389289.html
Copyright © 2011-2022 走看看