关于C#的委托与事件的一个小DEMO
2012-07-26 13:53
453 查看
从学习.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(13, 40, 0);
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;
}
}
}
学习委托与事件
参考张子阳 C# 中的委托和事件
以下是我写的一个小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(13, 40, 0);
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;
}
}
}
学习委托与事件
参考张子阳 C# 中的委托和事件
相关文章推荐
- C# 关于委托和事件的妙文:通过一个例子详细介绍委托和事件的作用;Observer模式简介
- C# 关于委托和事件的妙文:通过一个例子详细介绍委托和事件的作用;Observer模式简介
- 一个关于委托、事件、多线程、异步的Demo
- C# 关于委托和事件的妙文:通过一个例子详细介绍委托和事件的作用;Observer模式简介
- [转摘] 关于C#中委托和事件机制的一个最佳实例
- C# 中关于委托和事件的一个实例
- DEMO[C#]TypeChange 关于类型转换的一个DEMO
- C#学习笔记(三)—–C#高级特性中的委托与事件:关于事件
- c#关于委托和事件(二)(介绍的很详细)
- c#中委托的概念一个总结demo
- c#关于委托和事件(一)(介绍的很详细)
- 关于委托与事件的一个小例子
- DEMO[C#]TypeChange 关于类型转换的一个DEMO
- .NET委托:一个关于C#的睡前故事
- (每日心得)关于C#中委托与事件的一些认识(一)
- 关于c#中委托与事件的一些理解
- 关于 C# 中的 Struct 的一个简单的 Demo
- 关于C#/.NET中委托和事件的机制和应用的故事(转)
- .NET委托:一个关于C#的睡前故事
- 关于C#的委托和事件的一点思考