C#事件与代理的简单运用

来源:互联网 发布:屏蔽软件弹出广告 编辑:程序博客网 时间:2024/05/29 10:59
#region Using directivesusing System;using System.Collections.Generic;using System.Text;#endregionnamespace CarEvents{    public class Car    {        #region Basic Car state data / constructors        // Internal state data.        public int CurrentSpeed { get; set; }        public int MaxSpeed { get; set; }        public string PetName { get; set; }        // Is the car alive or dead?        private bool carIsDead;        public Car()        {            MaxSpeed = 100;        }        public Car(string name, int maxSp, int currSp)        {            CurrentSpeed = currSp;            MaxSpeed = maxSp;            PetName = name;        }        #endregion        // This delegate works in conjunction with the        // Car's events.        public delegate void CarEngineHandler(string msg);        // This car can send these events.        public event CarEngineHandler Exploded;        public event CarEngineHandler AboutToBlow;        public void Accelerate(int delta)        {            // If the car is dead, fire Exploded event.            if (carIsDead)            {                if (Exploded != null)                    Exploded("Sorry, this car is dead...");            }            else            {                CurrentSpeed += delta;                // Almost dead?                if (10 == MaxSpeed - CurrentSpeed                  && AboutToBlow != null)                {                    AboutToBlow("Careful buddy!  Gonna blow!");                }                // Still OK!                if (CurrentSpeed >= MaxSpeed)                    carIsDead = true;                else                    Console.WriteLine("CurrentSpeed = {0}", CurrentSpeed);            }        }    }}
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace CarEvents{    class Program    {        #region Main() not using method group conversion        //static void Main(string[] args)        //{        //    Console.WriteLine("***** Fun with Events *****\n");        //    Car c1 = new Car("SlugBug", 100, 10);        //    // Register event handlers.        //    c1.AboutToBlow += new Car.CarEngineHandler(CarIsAlmostDoomed);        //    c1.AboutToBlow += new Car.CarEngineHandler(CarAboutToBlow);        //    Car.CarEngineHandler d = new Car.CarEngineHandler(CarExploded);        //    c1.Exploded += d;        //    Console.WriteLine("***** Speeding up *****");        //    for (int i = 0; i < 6; i++)        //        c1.Accelerate(20);        //    // Remove CarExploded method        //    // from invocation list.        //    c1.Exploded -= d;        //    Console.WriteLine("\n***** Speeding up *****");        //    for (int i = 0; i < 6; i++)        //        c1.Accelerate(20);        //    Console.ReadLine();        //}         #endregion        static void Main(string[] args)        {            Console.WriteLine("***** Fun with Events *****\n");            Car c1 = new Car("SlugBug", 100, 10);            // Register event handlers.            c1.AboutToBlow += CarAboutToBlow;            c1.AboutToBlow += CarIsAlmostDoomed;                        c1.Exploded += CarExploded;            Console.WriteLine("***** Speeding up *****");            for (int i = 0; i < 6; i++)                c1.Accelerate(20);            c1.Exploded -= CarExploded;            Console.WriteLine("\n***** Speeding up *****");            for (int i = 0; i < 6; i++)                c1.Accelerate(20);            Console.ReadLine();        }        #region Targets for events        public static void CarAboutToBlow(string msg)        { Console.WriteLine(msg); }        public static void CarIsAlmostDoomed(string msg)        { Console.WriteLine("=> Critical Message from Car: {0}", msg); }        public static void CarExploded(string msg)        { Console.WriteLine(msg); }        #endregion    }}