委托与事件

ContractedBlock.gif ExpandedBlockStart.gif Code
//内置的委派类型-----事件处理器
using System;
class UsingEventHandler
{
    
private void AddMethod()
    {
        Console.WriteLine (
"asdfafsdafasfas");
        }
    
private void DoAdd(object o,EventArgs e)
    {
        Console.WriteLine (
"chufa:{0}",o.ToString());
        AddMethod();    
        }
    
static void Main()
    {
        UsingEventHandler myevent
=new UsingEventHandler();
        calc mycalc
=new calc();
        mycalc.evtDocalc
+=new EventHandler(myevent.DoAdd);
        Console.WriteLine (
"----------------");
        mycalc.DoCalc();
        Console.Read ();
                                               
      }
}
class calc
{
  
public event EventHandler evtDocalc;
  
public void DoCalc()
  {
      
/*指定其支持的委派为EventHandler,而触发的evtDocalc的程序代码,
        必须传入两个指定类型的参数,第一个为目前的类对象,因此直接传入this关键字,
        EventHandler由于不包含事件的相关信息,因此传入EventArgs类的Empty属性值
*/
      evtDocalc(
this,EventArgs.Empty);  
  }
}
-------------------------------------------------------------------------
using System;
using System.Collections.Generic;

namespace DelegateEvent
{
    
class MainClass
    {
        
public static void Main(string[] args)            
        {
                Console.WriteLine (
"多重传送委派");
            PrintSave ps
=new PrintSave();
            DelegatePrintSave delegateps
=null;
            delegateps
+=new DelegatePrintSave(ps.Print);
            delegateps
+=new DelegatePrintSave(ps.Save );
            delegateps();
            Console.WriteLine (
"多重传送事件");
            EventAddSub eventaddsub
=new EventAddSub();
            eventaddsub.px
=10;
            eventaddsub.py
=5;
            AddSub addsub
=new AddSub();
                        
//向事件中加入两个委派的方法的引用
            eventaddsub.doEvent+=new delegateAddSub(addsub.Add);
            eventaddsub.doEvent
+=new delegateAddSub(addsub.Sub);
                        
//引用OnEvent方法时,触发doEvent
            eventaddsub.OnEvent();
            Console.Read();
        
            
            
        }
    }
    
public delegate void DelegatePrintSave();
    
public class PrintSave
    {
        
public void Print()
        {
            Console.WriteLine (
"Finished Save! ,Starting Printing!");
            Console.WriteLine (
"File finished Printing!");        
                }
        
        
public void Save()
        {
            Console.WriteLine (
"Finished Edit! ,Starting Saving!");
            Console.WriteLine (
"File finished Saving!");    
                }
    }
    
    
public delegate void delegateAddSub(int x,int y);
    
public class AddSub
    {
        
public void Add(int x,int y)
        {
            
int bResult;
            bResult
=x+y;
            Console.WriteLine (
"{0}+{1}={2}",x,y,bResult);    
               }
        
public void Sub(int x,int y)
        {
            
int bResult;
            bResult
=x-y;
            Console.WriteLine (
"{0}-{1}={2}",x,y,bResult);        
                }    
       }
    
public class EventAddSub
    {
        
private int x;
        
private int y;
        
public int px
        {
            
set{x=value;}        
                }
        
        
public int py
        {
            
set{y=value;}    
               }
        
        
public event delegateAddSub doEvent;
        
public void OnEvent()
        {
            doEvent(x,y);
        }
            
        }
    
}

转载于:https://www.cnblogs.com/hubcarl/archive/2009/05/19/1460029.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值