zoukankan      html  css  js  c++  java
  • 委托与事件

    namespace CShapExc
    {
        class Program
        { 
            static void Main()
            {
                Heater h = new Heater();
                JBQ j = new JBQ();
                 h.BoilEven += j.MakeAlert;
                 h.BoilEven += j.DisplayInfo;
                h.BoilWater();
                Console.ReadKey();
            }
        }
        public class Heater
        {
            private int wenDu;
            public delegate void BoilHandle(int para);//声明委托
            public event BoilHandle  BoilEven;//声明事件
            /// <summary>
            /// 烧水
            /// </summary>
            public void BoilWater()
            {
                for (int i = 0; i <= 110; i++)
                {
                    wenDu = i;
                    if (wenDu > 95)
                    {
                        if (BoilEven != null)//如果有对象注册
                        {
                            BoilEven(wenDu);//调用所有注册对象的方法
                        }
                    }
                }
            }
        }
        /// <summary>
        /// 警报器
        /// </summary>
        public class JBQ
        {
            /// <summary>
            /// 报警
            /// </summary>
            /// <param name="param"></param>
            public void MakeAlert(int param)
            {
                Console.WriteLine("警报警报:温度已经{0}度了",param);
            }
            /// <summary>
            /// 显示信息
            /// </summary>
            /// <param name="param"></param>
            public void DisplayInfo(int param)
            {
                Console.WriteLine("当前温度:{0}度",param);
            }
        }
    }
  • 相关阅读:
    1697 ⑨要写信
    1220 数字三角形
    4979 数塔
    bzoj1618[Usaco2008 Nov]Buying Hay 购买干草
    bzoj1066[SCOI2007]蜥蜴
    bzoj1008[HNOI2008]越狱
    cf437D The Child and Zoo
    cf437C The Child and Toy
    cf437B The Child and Set
    cf437A The Child and Homework
  • 原文地址:https://www.cnblogs.com/xiexingen/p/2846518.html
Copyright © 2011-2022 走看看