C#事件_委托01(事件)

事件(Event)
事件是基于委托,为委托提供了一个发布、订阅机制,可以说事件是一种具有特殊签名的委托;
事件的声明:

public event 委托类型 事件名
事件使用event关键词来声明,他的返回值是一个委托类型。
通常事件的命名,以名字+Event作为他的名称,在编码中尽量使用规范命名。

下面是通过委托的形式实现观察者设计的一个例子:

using System;
namespace SimpleEvent
{


class Cat{
      private string name;
      private string color;
      public Action MouseMove;
      public Cat(string name, string color)
      {
          this.name = name;
          this.color = color;
      }

      public void CatCome()
      {
          Console.WriteLine(this.color+"的猫 它的名字叫" + this.name + " 来了哦 !");
          if(MouseMove != null)
          {
              MouseMove();
          }
      }

      
  }


class Mouse{
    string name,color;
    public Mouse(string name, string color)
    {
        this.name = name;
        this.color  = color;
    }

    public void MouseRun()
    {
        
        Console.WriteLine(this.color+"的老鼠 它的名字叫" + this.name + " 开始逃跑 !");
    }
}

public class MainClass
  {
    public static void Main(string[] args)
    {
      Cat smartCat = new Cat("元宝","深蓝色");
      
      Mouse mouse1 = new Mouse("小明","白");
      Mouse mouse2 = new Mouse("小红","红");
      Mouse mouse3 = new Mouse("小刘","黑");
      smartCat.MouseMove += mouse1.MouseRun;
      smartCat.MouseMove += mouse2.MouseRun;
      smartCat.MouseMove += mouse3.MouseRun;


      smartCat.CatCome();

    }
  }

}

下面是使用事件来处理响应机制:

using System;
namespace SimpleEvent
{


class Cat{
      private string name;
      private string color;
      public event Action MouseMoveEvent;
      public Cat(string name, string color)
      {
          this.name = name;
          this.color = color;
      }

      public void CatCome()
      {
          Console.WriteLine(this.color+"的猫 它的名字叫" + this.name + " 来了哦 !");
          if(MouseMoveEvent != null)
          {
              MouseMoveEvent();
          }
      }

      
  }


class Mouse{
    string name,color;
    public Mouse(string name, string color, Cat nowCat)
    {
        this.name = name;
        this.color  = color;
        nowCat.MouseMoveEvent += MouseRun;//把逃跑方法添加到猫的事件中,只要猫启动了这个事件,那么添加的方法都会启动。
    }

    public void MouseRun()
    {
        
        Console.WriteLine(this.color+"的老鼠 它的名字叫" + this.name + " 开始逃跑 !");
    }
}

public class MainClass
  {
    public static void Main(string[] args)
    {
      Cat smartCat = new Cat("元宝","深蓝色");
      
      Mouse mouse1 = new Mouse("小明","白", smartCat);
      Mouse mouse2 = new Mouse("小红","红", smartCat);
      Mouse mouse3 = new Mouse("小刘","黑", smartCat);
    //   smartCat.MouseMove += mouse1.MouseRun;
    //   smartCat.MouseMove += mouse2.MouseRun;
    //   smartCat.MouseMove += mouse3.MouseRun;


      smartCat.CatCome();
    //  smartCat.MouseMoveEvent() 这一行代码是不允许的,事件不能在类的外部触发,只能在类的内部触发。委托也尽量不要在外部触发。

    }
  }

}

总结:
1.定义一个事件需要做两件事件

  1. 定义一个委托
  2. 利用定义的委托定义事件

2.事件在哪个类注册的就相当于谁是发布者,就是只有这个类能启动这个事件。
3.什么是订阅者,订阅者就是有一个类,引入了2这个类,同时把自己的某个方法添加(+=)到了2类中的事件上。
4.此时闭环完场。就是只要2的事件被调用,那么就会启动这个事件中添加的方法。

invoke回调别的函数,可以与事件配合使用。
单独使用自己查
与事件一期使用:
**event?.invoke();//如果该事件不为空,则回调。省去了判断事件是否为空的判断

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

城墙郭外斜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值