c# 事件和委托

    // 1 事件和委托的区别
    // 2 简单的例子
    class Program
    {
        static void Main(string[] args)
        {
            {
                Cat cat = new Cat();
                cat.CatMiaoAction += new Dog().Wang;
                cat.CatMiaoAction += new Mother().Wispher;
                //cat.CatMiaoAction = null;   允许
                //cat.CatMiaoAction.Invoke();
                cat.MiaoDelegate();
            }
            {
                Cat cat = new Cat();
                cat.CatMiaoActionHandler += new Dog().Wang;
                cat.CatMiaoActionHandler += new Mother().Wispher;
                //cat.CatMiaoActionHandler = null;  event权限限制,不允许声明类外面的操作
                //cat.CatMiaoActionHandler.invoke();
                cat.MiaoEvent();
            }

            Console.ReadKey();
        }
    }
   public class Dog 
    {
        public void Wang()
        {
            Console.WriteLine("{0} Wang", this.GetType().Name);
        }
    }
   public class Mother
    {
        public void Wispher()
        {
            Console.WriteLine("{0} Wispher", this.GetType().Name);
        }
    }
 public class Cat
    {
        //委托的实例
        public Action CatMiaoAction;
        public void MiaoDelegate()
        {
            Console.WriteLine("{0} MiaoDelegate", this.GetType().Name);
            this.CatMiaoAction?.Invoke();
        }

        //事件event:一个委托的实例,带一个event关键字
        //限制权限,只允许在事件声明类里面去invoke和赋值,不允许外面,甚至子类
        //事件和委托的区别与联系? 
        //委托是一种类型,事件是委托类型的一个实例,加上了event的权限控制
        //Student是一种类型,Tony就是Student类型的一个实例,
        public event Action CatMiaoActionHandler;
        public void MiaoEvent()
        {
            Console.WriteLine("{0} MiaoEvent", this.GetType().Name);
            this.CatMiaoActionHandler?.Invoke();
        }
    }

一个例子

 public class EventStandard
    {
        public static void Show()
        {
            Pig lesson = new Pig()
            {
                Id = 123,
                Name = "猪肉价格",
                Price = 40
            };
            //订阅:把订户和发布者的事件关联起来
            lesson.IncreaseHandler += new Masses().Buy;
            lesson.IncreaseHandler += new TV().Popularize;
            lesson.Price = 50;
        }


        /// <summary>
        /// 订户:关注事件,事件发生后,自己做出对应的动作
        /// </summary>
        public class Masses
        {
            public void Buy(object sender, EventArgs e)
            {
                Pig pig = (Pig)sender;
                Console.WriteLine($"This is {pig.Name} Lesson");

                XEventArgs args = (XEventArgs)e;
                Console.WriteLine($"之前价格{args.OldPrice}");
                Console.WriteLine($"现在价格{args.NewPrice}");
                Console.WriteLine("果断买了!!!");
            }
        }
        public class TV
        {
            public void Popularize(object sender, EventArgs e)
            {
                Pig pig = (Pig)sender;
                Console.WriteLine($"This is {pig.Name} Lesson");

                XEventArgs args = (XEventArgs)e;
                Console.WriteLine($"之前价格{args.OldPrice}");
                Console.WriteLine($"现在价格{args.NewPrice}");
                Console.WriteLine("广大用户请留意!!!");
            }
        }

        /// <summary>
        /// 事件参数  一般会为特定的事件去封装个参数类型
        /// </summary>
        public class XEventArgs : EventArgs
        {
            public int OldPrice { get; set; }
            public int NewPrice { get; set; }
        }


        /// <summary>
        /// 事件的发布者,发布事件并且在满足条件的时候,触发事件
        /// </summary>
        public class Pig
        {
            public int Id { get; set; }
            public string Name { get; set; }

            private int _price;
            public int Price
            {
                get
                {
                    return this._price;
                }
                set
                {
                    if (value > this._price)
                    {
                        this.IncreaseHandler?.Invoke(this,
                            new XEventArgs()
                            {
                                OldPrice = this._price,
                                NewPrice = value
                            });
                        this._price = value;
                    }
                }
            }

            /// <summary>
            /// 降价事件
            /// 
            /// </summary>
            public event EventHandler IncreaseHandler;

        }
    }

 

posted @ 2019-11-07 14:12  王大师123  阅读(144)  评论(0编辑  收藏  举报