zoukankan      html  css  js  c++  java
  • 事件event

    委托,是事件基础

    类的对象,发出消息,在运行时绑定处理方法。

    以下,以animal为例,体温过高时,触发事件

    1 先定一个个委托

    delegate void MyDelegate();

    2 在类中定义事件,并在某时刻触发。此例中在体温属性改变,大于37.5时触发。

        class Animal
        {
            // 定义一个事件 体温过高  (先定义MyDelegate)
            public event MyDelegate highTemperature;
            float temperature;//体温

            public float Temperature
            {
                get { return temperature; }
                set
                {
                    temperature = value; 
                    //体温高时,触发事件。事件,一般在属性改变时触发
                    if (temperature > 37.5)
                    {
                        highTemperature();
                    }
                }
            }      
        }

    3 使用事件时,要将事件与处理事件的方法关联,然后改变属性触发事件。

    定义一个方法,热了,就开空调

            static void a_highTemperature()
            {
                Console.WriteLine("开空调");
            }

    然后在main方法中写

                Animal a = new Animal();
                a.highTemperature += new MyDelegate(a_highTemperature); //事件 --- 处理方法
                a.Temperature = 37.6f;
  • 相关阅读:
    p4841 城市规划
    p2619 [国家集训队2]Tree I [wqs二分学习]
    p3723 [AH2017/HNOI2017]礼物
    p5437 【XR-2】约定
    p5349 幂
    数据结构:结构之美
    你所不知道的传输层
    为什么选择这种技术而不选择另一种技术?
    Internet路由-主机路由表和转发表
    计算机网络----数据链路层(三)
  • 原文地址:https://www.cnblogs.com/imxh/p/2442485.html
Copyright © 2011-2022 走看看