赵星汉同学的技术博客 Software testing & software engineering

C#里的事件

2021-07-29
Frank Xinghan Zhao


如何使用C#事件的笔记

Why do we use event

前边在委托中,委托的成员变量不是私有的,包括注册等还存在重复代码,代码不简洁,而使用事件,就能避免这类的问题。

How do we use event

1)在类定义中,在委托定义之后,定义事件

public event 委托名称 事件名称

2)设定对象发送事件,就不用注册函数了。具体使用和委托类似,但是代码简洁了很多。

CODE

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

namespace test3
{
    public class Car
    {
        public int CurrentSpeed { get; set; }
        public int MaxSpeed { get; set; }
        public string PetName{ get; set; }

        private bool carIsDead;

        public Car() { MaxSpeed = 100; }
        public Car( string name, int maxSp, int currSp)
        {
            CurrentSpeed = currSp;
            MaxSpeed = maxSp;
            PetName = name;
        }

        public delegate void CarEngineHandler(string msgForCaller);

        public event CarEngineHandler Exploded;

        public event CarEngineHandler AboutToBlow;

        public void Accelerate(int delta)
        {
            if (carIsDead)
            {
                if (Exploded != null)   //notice here
                {
                    Exploded("Sorry, this car is dead ...");
                }
            }
            else
            {
                CurrentSpeed += delta;

                if (10 == (MaxSpeed - CurrentSpeed) && AboutToBlow != null)
                {
                    AboutToBlow("Careful buddy! Gonna blow!");
                }

                if (CurrentSpeed >= MaxSpeed)
                    carIsDead = true;
                else
                    Console.WriteLine("CurrentSpeed = {0}", CurrentSpeed);
            }
        }
    }
  
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("****  Delegates as event enablers ******\n");

            Car c1 = new Car("SlugBug", 100, 10);

            //register event dealing program
            c1.AboutToBlow += CarIsAlmostDoomed;
            c1.AboutToBlow += CarAboutToBlow;

            Car.CarEngineHandler d = CarExploded;
            c1.Exploded += d;

            Console.WriteLine("**** Speeding up *****");

            for(int i = 0; i< 6;i ++)
            {
                c1.Accelerate(20);
            }
            Console.ReadLine();


            c1.Exploded -= d;

            Console.WriteLine("****** Speeding up*******");
            for(int i  = 0; i<6; i++)
            {
                c1.Accelerate(20);
            }

            Console.ReadLine();
        }

        public static void OnCarEngineEvent(string msg)
        {
            Console.WriteLine("\n ******** Message From Car Object *********");
            Console.WriteLine(" => {0}", msg);
            Console.WriteLine("******************\n");
        }

        public static void OnCarEngineEvent2(string msg)
        {
            Console.WriteLine("=>{0}", msg.ToUpper());

        }

        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);
        }
    }
}

Comments

Content