C#之事件学习

using System;
using System.IO;

namespace BoilerEventApp1
{

    //boiler类
    class Boiler
    {
        private int temp;
        private int pressure;
        public Boiler(int t,int p)
        {
            temp = t;
            pressure = p;
        }


        public int getTemp()
        {
            return temp;
        }

        public int getPressure()
        {
            return pressure;
        }
    }


    //事件发布器 
    class DelegateBoilerEvent
    {
        public delegate void BoilerLogHandler(string status);

        //基于上面的委托定义事件
        public event BoilerLogHandler BoilerEventLog;

        public void LogProcess()
        {
            string remarks = "O. K";
            Boiler b = new Boiler(100, 120);
            int t = b.getTemp();
            int p = b.getPressure();

            if(t>150||t<80||p<12||p>15)
            {
                remarks = "Need Maintenance";
            }

            OnBoilerEventLog("Logging Info : \n");
            OnBoilerEventLog("Temparature " + t + "\nPressure: " + p);
            OnBoilerEventLog("\nMessage: " + remarks);
        }

        protected void OnBoilerEventLog(string message)
        {
            if(BoilerEventLog != null)
            {
                BoilerEventLog(message);
            }
        }
    }



    //该类保留写入日志文件的条款
    class BoilerInfoLogger
    {
        FileStream fs;
        StreamWriter sw;

        public BoilerInfoLogger(string filename)
        {
            fs = new FileStream(filename, FileMode.Append, FileAccess.Write);
            sw = new StreamWriter(fs);
        }


        public void Logger(string info)
        {
            sw.WriteLine(info);
        }

        public void Close()
        {
            sw.Close();
            fs.Close();
        }
    }


    //事件订阅器
    public class RecordBoilerInfo
    {
        static void Logger(string info)
        {
            Console.WriteLine(info);
        }

        static void Main(string[] args)
        {
            BoilerInfoLogger filelog = new BoilerInfoLogger(
                "C:\\Users\\Rooobins\\Desktop\\boiler.txt");                      //创建写入日志的文件
            DelegateBoilerEvent boilerEvent = new DelegateBoilerEvent();          //实例化事件发生器
            boilerEvent.BoilerEventLog += new                                      /*基于委托定义的事件*/
                DelegateBoilerEvent.BoilerLogHandler(Logger);                     //事件发生器之委托  

            boilerEvent.BoilerEventLog += new
                DelegateBoilerEvent.BoilerLogHandler(filelog.Logger);

            boilerEvent.LogProcess();                                             //事件发生器,反应写入过程
            Console.ReadLine();
            filelog.Close();
        }
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值