c#的事件机制示例代码: 猫-> 老鼠, 主人

None.gif using  System;
None.gif
None.gif
namespace  EventDemo
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
InBlock.gif
InBlock.gif    
public delegate void CatBrayEventHandle(int score);
InBlock.gif
InBlock.gif
InBlock.gif    
public class sleeping
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public event CatBrayEventHandle CatShout;
InBlock.gif
InBlock.gif        
int theScore;
InBlock.gif
InBlock.gif        
public int Score
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gif{return theScore;}
InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(theScore != value)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    CatShout(value);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif    
public class MouseRun
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public MouseRun(sleeping slp)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            slp.CatShout 
+= new CatBrayEventHandle(slp_CatShout);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void slp_CatShout(int score)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(score > 100)
InBlock.gif                Console.WriteLine(
"老鼠: 有猫!快逃啊dot.gif..");
InBlock.gif            
else
InBlock.gif                Console.WriteLine(
"老鼠: 猫虽然叫了, 声音不够大, 赫赫, 老鼠没听见!");
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif
InBlock.gif    
public class Master
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public Master(sleeping sleep)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            sleep.CatShout 
+= new CatBrayEventHandle(sleep_CatShout);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void sleep_CatShout(int score)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(score > 100)
InBlock.gif                Console.WriteLine(
"主人: 猫叫了!");
InBlock.gif            
else
InBlock.gif                Console.WriteLine(
"主人: 猫虽然叫了, 声音不够大, 主人没听见!");
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Class1 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    class Class1
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 应用程序的主入口点。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [STAThread]
InBlock.gif        
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            sleeping slping 
= new sleeping();
InBlock.gif
InBlock.gif            MouseRun mr 
= new MouseRun(slping);
InBlock.gif            Master ms 
= new Master(slping);
InBlock.gif
InBlock.gif            
//slping.Score = 66;
InBlock.gif
            slping.Score = 123;
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/silva/archive/2006/04/09/370565.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值