c#自定义事件


例子:变色龙随着环境温度变化,改变自身颜色。

事件:环境的温度改变,所以应该有一个环境类,定义环境改变事件,并定义触发事件的方法;

事件处理:变色龙根据温度,判断自身颜色变化,所以变色龙类有根据环境温度改变颜色的事件处理方法。

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

namespace EventHandler
{ 
   //1.自定义事件参数,若没有参数,可以省略这一步
    public class ChangeEnvironmentEventArg : EventArgs
    {
        int temperature;
        public ChangeEnvironmentEventArg(int temperature)
        {
            this.temperature = temperature;
        }
        public int Temperature
        {
            get { return temperature; }
        }

    }

    public class Environment
    {
        //2/定义事件委托
        public delegate void ChangeEnvironmentEventHandler(object sender, ChangeEnvironmentEventArg eventArg);
        //4.定义事件
        public event ChangeEnvironmentEventHandler changeEnvironment;

        //4.触发事件
        public void OnChangeEnvironment(int t)
        {
            if(changeEnvironment != null)
            changeEnvironment(this , new ChangeEnvironmentEventArg(t));
        }
    }

      class Chameleon
    {
          string color = "green";
          string name;
         public  Chameleon(string name)
          {
              this.name = name;
          }

        //5.自定义事件处理
        //变色龙,随着环境变化,改变颜色
        public void ChangeColor(object sender, ChangeEnvironmentEventArg eventArg)
        {
            if (eventArg.Temperature <  30)
            {
                
                color = "green";
            }
            else if(eventArg.Temperature > 50)
            {
                color = "orange";
            }else{
                color = "red";
            }
            Console.WriteLine("{0}:The Envirment temperature is {1}, i am changing color to {2}",name,eventArg.Temperature,color);
        }

        public string Color
        {
            get { return color;  }
        }
        public void printColor()
        {
            Console.WriteLine("{0}'s color is {1}  ",name, color);
        } 

    }

    class Program
    {

       
        static void Main(string[] args)
        {
            Chameleon chameleon1 = new Chameleon("chameleon1");
            Chameleon chameleon2 = new Chameleon("chameleon2");


            Environment environment = new Environment();
            //6.订阅事件
            environment.changeEnvironment += chameleon1.ChangeColor;
            environment.OnChangeEnvironment(60); //触发事件

            chameleon1.printColor();
            chameleon2.printColor();

            Console.ReadKey();
        }
    }


}

只有变色龙1订阅了环境改变事件,所以只有他可以根据温度改变颜色。如何让所有的变色龙都对环境温度敏感,做出反应呢?

可以声明事件为static event,在变色龙构造函数中,订阅事件,就可以让每个变色龙都订阅环境改变事件了。

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

namespace EventHandler
{ 
   //1.自定义事件参数
    public class ChangeEnvironmentEventArg : EventArgs
    {
        int temperature;
        public ChangeEnvironmentEventArg(int temperature)
        {
            this.temperature = temperature;
        }
        public int Temperature
        {
            get { return temperature; }
        }

    }

    public class Environment
    {
        //2/定义事件委托
        public delegate void ChangeEnvironmentEventHandler(object sender, ChangeEnvironmentEventArg eventArg);
        //4.定义事件
        public static event ChangeEnvironmentEventHandler changeEnvironment;

        //4.触发事件
        public void OnChangeEnvironment(int t)
        {
            if(changeEnvironment != null)
            changeEnvironment(this , new ChangeEnvironmentEventArg(t));
        }
    }

      class Chameleon
    {
          string color = "green";
          string name;
         public  Chameleon(string name)
          {
            this.name = name;
            //6.订阅事件
            Environment.changeEnvironment += this.ChangeColor;
          }

        //5.自定义事件处理
        //变色龙,随着环境变化,改变颜色
        public  void ChangeColor(object sender, ChangeEnvironmentEventArg eventArg)
        {
            if (eventArg.Temperature <  30)
            {
                
                color = "green";
            }
            else if(eventArg.Temperature > 50)
            {
                color = "orange";
            }else{
                color = "red";
            }
            Console.WriteLine("{0}:The Envirment temperature is {1}, i am changing color to {2}",name,eventArg.Temperature,color);
        }

        public string Color
        {
            get { return color;  }
        }
        public void printColor()
        {
            Console.WriteLine("{0}'s color is {1}  ",name, color);
        } 

    }

    class Program
    {

       
        static void Main(string[] args)
        {
            Chameleon chameleon1 = new Chameleon("chameleon1");
            Chameleon chameleon2 = new Chameleon("chameleon2");

            Environment environment = new Environment();
            environment.OnChangeEnvironment(60); //触发事件

            chameleon1.printColor();
            chameleon2.printColor();

            Console.ReadKey();
        }
    }


}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值