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;
  • 相关阅读:
    zoj 3593 One Person Game
    poj 2115 C Looooops
    hdu 1576 A/B
    hdu 2669 Romantic
    poj1006 Biorhythms
    中国剩余定理(孙子定理)
    Pseudoprime numbers---费马小定理
    青蛙的约会----POJ1061
    [POJ2942]:Knights of the Round Table(塔尖+二分图染色法)
    [BZOJ1718]:[Usaco2006 Jan] Redundant Paths 分离的路径(塔尖)
  • 原文地址:https://www.cnblogs.com/imxh/p/2442485.html
Copyright © 2011-2022 走看看