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;
            }

        }

    }

     学习委托与事件

  • 相关阅读:
    MVC部署到IIS 出现未能加载文件或程序集“System.Web.Http.WebHost.....
    web在线聊天框滚动条自动在底部
    IE 浏览器下英文 微软雅黑 不起作用
    U3D MonoBehaviour.InvokeRepeating 和 MonoBehaviour.Invoke
    U3D 中关于相同的怪物不发生碰撞 或者是想让一些物体发生碰撞 又不想让一些物体发生碰撞
    U3D 2D中给精灵添加刚体后 发现精灵会倒 ..
    Axure教程 | 轻量级的后台原型框架
    Axure:侧导航收缩与展开
    SQLSERVER数据库调优
    MySQL用GROUP BY分组取字段最大值或最新一条
  • 原文地址:https://www.cnblogs.com/guolihao/p/2609944.html
Copyright © 2011-2022 走看看