zoukankan      html  css  js  c++  java
  • 关于C#的委托与事件的一个小DEMO

    从学习.NET到现在,也有快4年时间了,一切都是在不经意间忽略,写了几年的代码,委托与事件其实一直在用,可以真的有人让我为一个类写一个事件,我真的会犹豫一下要如何写。

    以下是我写的一个小DEMO。

    设定一个闹钟。

    namespace ConsoleApplication6
    {
        public delegate void BellEventHandler(object sender, BellEventArgs e);
        public class BellEventArgs : EventArgs
        {
            public readonly int h;
            public readonly int m;
            public readonly int s;
            public BellEventArgs(int h, int m, int s)
            {
                this.h = h;
                this.m = m;
                this.s = s;
            }
        }
        
        class Program
        {
            static void Main(string[] args)
            {
                NaoZhong t = new NaoZhong();
                t.SetBellTime(13400);
                t.Bell += new BellEventHandler(t_Bell);
                t.StartBell();
            }
            static void t_Bell(object sender, BellEventArgs e)
            {
                Console.Write("{0}:{1}:{2}了,懒猪起床了", e.h.ToString(), e.m.ToString(), e.s.ToString());
                Console.Read();
            }

        }
        public class NaoZhong
        {
            private int Hours = 0;
            private int Minutes = 0;
            private int Seconds = 0;

            public event BellEventHandler Bell;

            public void StartBell()
            {
                while (!(DateTime.Now.Hour == Hours && DateTime.Now.Minute == Minutes && DateTime.Now.Second == Seconds))
                {


                }
                BellEventArgs e = new BellEventArgs(Hours, Minutes, Seconds);
                Bell(this, e);
            }
            public bool SetBellTime(int _h, int _m, int _s)
            {
                if (_h > 24)
                {
                    return false;
                }
                if (_m > 60)
                {
                    return false;
                }
                if (_s > 60)
                {
                    return false;
                }
                Hours = _h;
                Minutes = _m;
                Seconds = _s;
                return true;
            }

        }

    }

     学习委托与事件

  • 相关阅读:
    南阳33(蛇形填数)规律题;
    南阳241(字母统计)
    南阳57(6174问题)
    android图形基础知识
    Linux中yum手动安装、手动建立仓库文件夹关联实现关联包自动安装、yum相关命令使用
    debug连线指令
    Qt之信号连接,你Out了吗?
    hdu-4607-Park Visit
    MySQL 分区表 partition线上修改分区字段,后续进一步学习partition (1)
    如何用正则将多个空格看成一个空格结合spllit()方法将文本数据入库
  • 原文地址:https://www.cnblogs.com/guolihao/p/2609944.html
Copyright © 2011-2022 走看看