zoukankan      html  css  js  c++  java
  • 托付和事件的使用

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;


    namespace delegatedemo
    {
        public delegate void BoilDelegate(int temp);
        public class Heater
        {
            public int temprature;
            public event BoilDelegate boilEvent;
            public void Boil()
            {
                for (int i = 0; i <= 100; i++)
                {
                    temprature = i;
                    if (temprature > 95)
                    {
                        if (boilEvent != null)
                        {
                            boilEvent(temprature);
                        }
                    }
                }
            }
        }

    }

    显示器和报警器

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;


    namespace delegatedemo
    {
        public class Alerm
        {
            public void MakeAlerm(int temp)
            {
                Console.WriteLine("滴滴滴,水已经{0}度!", temp);
            }
        }


        public class Display
        {
            public void ShowMsg(int temp)
            {
                Console.WriteLine("水温是{0}度", temp);
                Console.ReadKey();
            }
        }
    }


    调用:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;


    namespace delegatedemo
    {
        class Program
        {
            static void Main(string[] args)
            {
                heater = new Heater();
                heater.boilEvent += new Alerm().MakeAlerm;//注冊报警器发出警报的事件
                heater.boilEvent += new Display().ShowMsg;//注冊显示温度的事件
                heater.Boil();
            }
        }
    }

  • 相关阅读:
    ofbiz初级教程
    IBASE4J开发环境搭建
    Nginx在windows上安装 及 Nginx的配置及优化
    Windows里正确安装Zookeeper以服务运行
    分享一个完整的Mybatis分页解决方案
    jquery weui ajax滚动加载更多
    Spring+Mybatis+SpringMVC后台与前台分页展示实例
    Mysql怎么样避免全表扫描,sql查询优化
    SQL优化|Java面试题
    mysql 全表扫描、全索引扫描、索引覆盖(覆盖索引)
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/5131452.html
Copyright © 2011-2022 走看看