posts - 77, comments - 54, trackbacks - 0, articles - 0
  IT博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

关于事件与委派的理解!

Posted on 2006-05-31 11:36 东人EP 阅读(143) 评论(0)  编辑 收藏 引用 所属分类: .NET

应用程序利用所定义的方法执行某种操作,完成特定的工作,而委派本身则是一种能够引用特定方法的对象,因此,可以通过委派对象,执行所引用的方法。
以下是委派的简单例子:

 1 using  System;
 2
 3 namespace  UsingDelegates
 4 {
 5   ///   <summary>
 6   ///  Class1 的摘要说明。
 7   ///   </summary>

 8   class  UsingDelegates
 9   {
10    ///   <summary>
11    ///  应用程序的主入口点。
12    ///   </summary>

13   [STAThread]
14    static   void  Main( string [] args)
15    {
16     long  length1  =   10 ;
17     long  length2  =   15 ;
18    myDelegate myDelegate1  =   new  myDelegate(Measure.Square);
19    myDelegate myDelegate2  =   new  myDelegate(Measure.Circle);
20    Console.WriteLine( " 边长为{0}的正方形面积为:{1} " , length1, myDelegate1(length1).ToString());
21    Console.WriteLine( " 半径为{0}的圆形面积为:{1} " , length2, myDelegate2(length2).ToString());
22    Console.ReadLine();
23   }

24
25  }

26   //  定义一个委派
27   public   delegate   double  myDelegate( long  length);
28  
29   public   class  Measure
30   {
31    public   static   double  Square( long  length)
32    {
33     double  area;
34    area  =  Math.Pow(length,  2 );
35     return  area;
36   }

37   
38    public   static   double  Circle( long  length)
39    {
40     double  area;
41    area  =  ( 4 / 3 ) * Math.PI * Math.Pow(length,  3 );
42     return  area;
43   }

44  }

45 }

46
47
只有注册用户登录后才能发表评论。