C#复习(十五)事件一

     
  • 事件基于委托存在,事件是类的成员
  • 事件不能在类外调用,虽然我们可以在类中声明事件为public,但实际编译代码时永远都是private,并且是private的委托(事件的本质就是委托)
  • 定义事件处理方法,它与delegate对象具有相同的参数和返回值类型
  • 定义事件时,发生者首先要定义委托,然后根据委托定义事件:
   public delegate void SayHello(string name);
   public event SayHello hello;
  • 注册事件:
        public void ToHelloAmerican(string name)
        {
            Console.WriteLine(name+",Welcome To American");
        }
        public void ToHelloChinese(string name)
        {
            Console.WriteLine(name + ",欢迎来中国");
        }
        static void Main(string[] args)
        {
            Hello h = new Hello();
            h.hello += h.ToHelloAmerican;
            h.hello += h.ToHelloChinese;
            h.ToHello("Selinda");
            Console.ReadKey();
        }
  • 触发事件:事件只能被所属的类调用,可以通过其他类向事件注册方法
        public void ToHello(string name)
        {
            hello(name);
        }
-----------------------------------------------------------------------------------------------
    源代码如下:(类外注册事件)

    public delegate void SayHello(string name);

    class Hello
    {
        public event SayHello hello;

        public void ToHello(string name)
        {
            hello(name);
        }

        public void ToHelloAmerican(string name)
        {
            Console.WriteLine(name+",Welcome To American");
        }
        public void ToHelloChinese(string name)
        {
            Console.WriteLine(name + ",欢迎来中国");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Hello h = new Hello();
            h.hello += h.ToHelloAmerican;
            h.hello += h.ToHelloChinese;
            h.ToHello("Selinda");

            Console.ReadKey();
        }
    }
       C#复习(十五)事件一

-----------------------------------------观察者模式------------------------------------------

  • Observer设计模式是为了定义对象间的一种一对多的依赖关系,以便于当一个对象的状态改变时,其他依赖于它的对象会被自动告知并更新。Observer模式是一种松耦合的设计模式。
  • Observer设计模式中主要包括两类对象:观察者、被观察对象。
  • 以下代码:观察者(报警器、显示频)、被观察对象(温度)
    public delegate void BoilEventHandler(int temperature);

    class BoilWater
    {
        public event BoilEventHandler Boil;
        public int temperature = 0;
        public void OnBoilWater()
        {
            for (int i = 0; i < 101; i++)
            {
                temperature = i;
                if (temperature > 95&&Boil!=null)
                {
                    Boil(temperature);
                }
            }
        }
        public void Ring(int tem)
        {
            Console.WriteLine("报警器:"+tem);
        }
        public void Show(int tem)
        {
            Console.WriteLine("显示器:"+tem);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            BoilWater h = new BoilWater();
            h.Boil += h.Ring;
            h.Boil += h.Show;
            h.OnBoilWater();
            Console.ReadKey();
        }
    }
          C#复习(十五)事件一

  • 委托类型的名称都应该以EventHandler结束。
  • 委托的原型定义:有一个void返回值,并接受两个输入参数:一个Object 类型(观察者),一个 EventArgs类型(或继承自EventArgs,被观察对象)。
  • 事件的命名为 委托去掉 EventHandler之后剩余的部分。
  • 继承自EventArgs的类型应该以EventArgs结尾。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

烫青菜

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

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

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

打赏作者

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

抵扣说明:

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

余额充值