委托和事件

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

namespace EventHandler类的使用
{
    public delegate void EventHandler(object sender,EventArgs e);//声明一个信号灯委托
   
 //事件参数类与事件相关的附加信息使用该类定义和存储
   class LightEventArgs:EventArgs
    {
        public int seconds;
        public int Seconds
        {
            get { return seconds; }
        }
        public LightEventArgs(int seconds)
        {
            this.seconds = seconds;
        }
    }
    //发布者
    class TrafficLight
    {
        private bool color = false;//红灯true绿灯false
        public bool Color { get { return color; } }//灯颜色的属性

        public event EventHandler ONcolorChange;//发布事件
        public void ChangeColor(int seconds)//触发事件的方法
        {
            color = !color;
            Console.WriteLine(color ? "红灯" : "绿灯");
            //判断事件是否注册,注册者调用委托(注册:订阅者对象receiver可以注册事件)
            if (ONcolorChange != null)
            {
                ONcolorChange(this, new LightEventArgs(seconds));//事件没有附加信息,第二个事件参数置为空
            }
        }
    }
    class Car
    {//车辆是订阅者
        //车辆行驶状态,行驶为true
        private bool isRun = true;

        public virtual void LightColorChange(object s,EventArgs e) //委托处理方法
        {
         
            //红灯亮,并且正在行驶
            if (isRun&&((TrafficLight)s).Color)
            {
                isRun = false;
                Console.WriteLine("{0}停车,{1}秒后启动", this,((LightEventArgs)e).Seconds);
            }
            else if (!isRun &&!((TrafficLight)s).Color)
            {
                isRun =true;
                Console.WriteLine("{0}停车,{1}秒后通过", this, ((LightEventArgs)e).Seconds);
            }

        }
        public void dy(TrafficLight tl)
        {
            tl.ONcolorChange += LightColorChange;//订阅发布的事件,将委托处理方法注册到委托中
        }
        public void qxdy(TrafficLight tl)
        {
            tl.ONcolorChange-= LightColorChange;
        }
    }
    //对发布的事件进行订阅

    class Program
    {
        static void Main(string[] args)
        {
            TrafficLight TL = new TrafficLight();//实例化发布者
            Car car1 = new Car();//实例化订阅者
            car1.dy(TL);//向发布者注册事件
            //触发事件
            TL.ChangeColor(30);//事件发生,car1随之响应
            TL.ChangeColor(60);//事件发生,car1随之响应
            Console.ReadLine();
            Directory.CreateDirectory(@"c:\temp\xx");
        }
        

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

namespace ConsoleApplication1
{
    //定义和使用委托
    public delegate void DelegateName(int x, int y);
   
class Program
    {

        static void Main(string[] args)
        {
            me m = new me();
            DelegateName fun1 = new DelegateName(m.fun4);//委托调用实例方法
            fun1(2, 4);//委托调用
            fun1 = new DelegateName(fun2);
            fun1(2, 4);//委托调用
            fun1 = new DelegateName(fun3);
            fun1(2, 4);//委托调用
            Console.ReadLine();
        }

        private static void fun2(int x, int y)
        {
            Console.WriteLine("fun2方法" + (x + y));
        }

        private static void fun3(int x, int y)
        {
            Console.WriteLine("fun3方法" + (x - y));
        }
      
    }
    class me
    {
        public void fun4(int x, int y)
        {
            Console.WriteLine("fun4方法" + (x * y));
        }
    }

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

namespace 泛型委托
{
    //定义和使用委托
    public delegate void DelegateName<T>(T x, T y);

    class Program
    {
        static void Main(string[] args)
        {
            DelegateName<string>fun = delegate (string x, string y)
            {
                x = "Hello";y = "\t";
            Console.Write(x+y);
            };
            fun += delegate (string x, string y)
            {
                 x = "World"; y= " !";
                Console.WriteLine(x+y);
            };
            fun("","");
            Console.ReadLine();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 匿名方法
{
    //定义和使用委托
    public delegate void DelegateName(int x, int y);

    class Program
    {
        static void Main(string[] args)
        {
            //将方法直接封装到委托中,然后调用委托
            DelegateName fun = delegate (int x, int y)
              {
                  Console.WriteLine("第一个方法");
              };
            fun += delegate (int x, int y)
              { Console.WriteLine("第二个方法"); };
            fun += delegate (int x, int y)
                { Console.WriteLine("第三个方法"); };
            fun(5, 2);
            Console.ReadLine();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 事件的发布和订阅
{
  /// <summary>
  /// 因为在委托中使用=订阅委托时,会覆盖所有订阅,使最后一个订阅生效,其他都被覆盖无效,所以,我们使用事件(event)限制使用委托的方式
  /// 订阅事件的时候,必须使用"+=""-="才能通过编译
  /// </summary>
  /// <param name="color"></param>
    public delegate void LightEvent(bool color);//声明一个信号灯委托
    //发布者
    class TrafficLight
    {//红灯true绿灯false
        private bool color = false;
        public bool Color { get; set; }//灯颜色的属性

        public event LightEvent ONcolorChange;//发布事件
        public void ChangeColor()//触发事件的方法
        {
            color = !color;
            Console.WriteLine(color ? "红灯" : "绿灯");
            //判断事件是否注册,注册者调用委托(注册:订阅者对象receiver可以注册事件)
            if (ONcolorChange != null)
            {
                ONcolorChange(color);
            }
        }
    }
    class Car
    {//车辆是订阅者
        //车辆行驶状态,行驶为true
        private bool isRun = true;

        public virtual void LightColorChange(bool color) //委托处理方法
        {
            //红灯亮,并且正在行驶
            if (isRun && color)
            {
                isRun = false; Car car1 = new Car();
                Console.WriteLine("{0}停车", car1);
            }
            //绿灯亮并且未行驶
            if (!isRun && !color)
            {
                isRun = true;
                Console.WriteLine("{0}启动", this);
            }

        }
        public void dy(TrafficLight tl)
        {
            tl.ONcolorChange += LightColorChange;//订阅发布的事件,将委托处理方法注册到委托中
        }
    }
    //对发布的事件进行订阅

    class Program
    {
        static void Main(string[] args)
        {
            TrafficLight TL = new TrafficLight();
            Car car1 = new Car();
            car1.dy(TL);
            TL.ChangeColor();//事件发生,car1随之响应
            TL.ChangeColor();//事件发生,car1随之响应
            Console.ReadLine();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 委托的发布和订阅
{
    //事件是一种特殊类型的委托 
    //事件的发布订阅就是委托的发布订阅
    //发布订阅有发送者和若干接受者(订阅者)
    //声明委托
    public delegate void LightEvent(bool color);//声明一个信号灯委托
    //发布者
    class TrafficLight
    {//红灯true绿灯false
        private bool color = false;
        public bool Color { get; set; }//灯颜色的属性

        public  LightEvent ONcolorChange;//发布委托
        public void ChangeColor()//触发事件的方法
        {
            color = !color;
            Console.WriteLine(color?"红灯":"绿灯");
            //判断事件是否注册,注册者调用委托(注册:订阅者对象receiver可以注册事件)
            if (ONcolorChange != null)
            {
                ONcolorChange(color);
            }   
        }
    }
    class Car
    {//车辆是订阅者
        //车辆行驶状态,行驶为true
        private bool isRun = true;
       
        public virtual void LightColorChange(bool color) //委托处理方法
        {
            //红灯亮,并且正在行驶
            if (isRun&&color)
            {
                isRun = false;Car car1 = new Car();
                Console.WriteLine("{0}停车",car1);
            }
            //绿灯亮并且未行驶
            if (!isRun&&!color)
            {
                isRun = true;
                Console.WriteLine("{0}启动",this);
            }
            
        }
        public void dy(TrafficLight tl)
        {
            tl.ONcolorChange += LightColorChange;//订阅发布的事件,将委托处理方法注册到委托中
            tl.ONcolorChange -= LightColorChange;//取消订阅发布的事件
        }
    }
    //对发布的事件进行订阅

    class Program
    {
        static void Main(string[] args)
        {
            TrafficLight TL = new TrafficLight();
            Car car1 = new Car();
            car1.dy(TL);
            TL.ChangeColor();//事件发生,car1随之响应
            TL.ChangeColor();//事件发生,car1随之响应
            Console.ReadLine();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 组合委托
{
    //定义和使用委托
    public delegate void DelegateName(int x, int y);

    class Program
    {

        static void Main(string[] args)
        {
            me m = new me();
            DelegateName fun1 = new DelegateName(m.fun4);//委托调用实例方法
     
            fun1 += new DelegateName(fun2);
    
            fun1 += new DelegateName(fun3);
            fun1(2, 4);//组合委托调用--依次调用
            Console.ReadLine();
        }

        private static void fun2(int x, int y)
        {
            Console.WriteLine("fun2方法" + (x + y));
        }

        private static void fun3(int x, int y)
        {
            Console.WriteLine("fun3方法" + (x - y));
        }

    }
    class me
    {
        public void fun4(int x, int y)
        {
            Console.WriteLine("fun4方法" + (x * y));
        }
    }

}

 

posted @ 2024-04-09 21:28  困到很想醒  阅读(7)  评论(0编辑  收藏  举报