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

#region Using directives

using System;
using System.Collections.Generic;
using System.Text;

#endregion

namespace 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
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值