注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

随机过程

http://superware.blog.163.com

 
 
 

日志

 
 

C# Event (II) : What's Event  

2014-06-17 17:22:16|  分类: 默认分类 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

1. Event
 

using System;
using System.IO;

namespace BoilerEventApplication
{
class BoilerEvent
{
public delegate void Delegate (string s);
public event Delegate d;

public void Process ()
{
OnEvent ("Boiler Infomation:");
OnEvent ("Temperature:");
OnEvent ("Pressure:");
}

private void OnEvent (string msg) // process something on event
{

// do something here

// ....

// ....


if (d != null)
d (msg);
else
Console.WriteLine ("Delegate is NULL...");
}
}

class PrintMessage
{
public void PrintMsg (string msg)
{
Console.WriteLine (msg);
}
}

public class BoilerEventMain
{
static void Main(string[] args)
{
BoilerEvent b = new BoilerEvent ();
PrintMessage p = new PrintMessage ();

Console.WriteLine ("");
b.d += new BoilerEvent.Delegate (p.PrintMsg); // register a event
b.Process (); // process something

// if some event registered

Console.WriteLine ("");
b.d -= new BoilerEvent.Delegate (p.PrintMsg); // remove a event
b.Process ();

Console.WriteLine ("");
b.d -= new BoilerEvent.Delegate (p.PrintMsg);
b.Process ();

Console.WriteLine ("");
b.d -= new BoilerEvent.Delegate (p.PrintMsg);
b.Process ();

Console.WriteLine ("");
b.d += new BoilerEvent.Delegate (p.PrintMsg);
b.Process ();

Console.WriteLine ("");
}
}
}


Boiler Infomation:
Temperature:
Pressure:

Delegate is NULL...
Delegate is NULL...
Delegate is NULL...

Delegate is NULL...
Delegate is NULL...
Delegate is NULL...

Delegate is NULL...
Delegate is NULL...
Delegate is NULL...

Boiler Infomation:
Temperature:
Pressure:



using System;
using System.IO;

namespace BoilerEventApplication
{
class Boiler
{
private double temp;
private double pres;

public Boiler (double t, double p)
{
temp = t;
pres = p;
}

public double Temp
{
set {temp = value;}
get {return temp;}
}

public double Pres
{
set {pres = value;}
get {return pres;}
}
}

class BoilerEvent
{
public delegate void Delegate (string s);
public event Delegate evnt;

// Process something here
public void Process ()
{
Boiler b = new Boiler (12, 120);

double t = b.Temp;
double p = b.Pres;
OnEvent ("Boiler Infomation:");
OnEvent ("Temperature: " + t);
OnEvent ("Pressure: " + p);

b.Temp = 15;
b.Pres = 150;

t = b.Temp;
p = b.Pres;
OnEvent ("Boiler Infomation:");
OnEvent ("Temperature: " + t);
OnEvent ("Pressure: " + p);
}

private void OnEvent (string s)
{
if (evnt != null)
evnt (s);
else
Console.WriteLine ("Delegate is NULL...");
}
}

class PrintMessage
{
public void PrintMsg (string s)
{
Console.WriteLine (s);
}

public void PrintMsg (string s)
{
Console.WriteLine (s);
}
}

public class BoilerEventMain
{
static void Main(string[] args)
{
BoilerEvent b = new BoilerEvent ();
PrintMessage p = new PrintMessage ();

Console.WriteLine ("");
b.evnt += new BoilerEvent.Delegate (p.PrintMsg);
b.Process ();

Console.WriteLine ("");
b.evnt -= new BoilerEvent.Delegate (p.PrintMsg);
b.Process ();

Console.WriteLine ("");
b.evnt -= new BoilerEvent.Delegate (p.PrintMsg);
b.Process ();

Console.WriteLine ("");
b.evnt -= new BoilerEvent.Delegate (p.PrintMsg);
b.Process ();

Console.WriteLine ("");
b.evnt += new BoilerEvent.Delegate (p.PrintMsg);
b.Process ();

Console.WriteLine ("");
}
}
}


Boiler Infomation:
Temperature: 12
Pressure: 120
Boiler Infomation:
Temperature: 15
Pressure: 150

Delegate is NULL...
Delegate is NULL...
Delegate is NULL...
Delegate is NULL...
Delegate is NULL...
Delegate is NULL...

Delegate is NULL...
Delegate is NULL...
Delegate is NULL...
Delegate is NULL...
Delegate is NULL...
Delegate is NULL...

Delegate is NULL...
Delegate is NULL...
Delegate is NULL...
Delegate is NULL...
Delegate is NULL...
Delegate is NULL...

Boiler Infomation:
Temperature: 12
Pressure: 120
Boiler Infomation:
Temperature: 15
Pressure: 150

  评论这张
 
阅读(57)| 评论(0)
推荐

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017