现在的位置: 首页 > 综合 > 正文

delegate——委派

2013年07月16日 ⁄ 综合 ⁄ 共 3022字 ⁄ 字号 评论关闭
 1using System;
 2namespace uu
 3{
 4 public class Delegates
 5 {
 6  public static void Main()
 7  {
 8   long length;
 9   long width;
10   long height;
11   Console.Write("请输入长度:");
12   length=long.Parse(Console.ReadLine());
13   Console.Write("请输入宽度");
14   width=long.Parse(Console.ReadLine());
15   Console.Write("请输入高度");
16   height=long.Parse(Console.ReadLine());
17   GGResult myGetResult=new GGResult(height,length,width);
18   Measure myMeasure=new Measure();
19   GGResult.MyDelegate myGRdelegate;
20   if(length==width)
21   {
22    myGRdelegate=new GGResult.MyDelegate(myMeasure.MeasureSquare);//提示要参数
23   }

24   else
25   {
26    myGRdelegate=new GGResult.MyDelegate(myMeasure.MeasureRectangle);//提示要参数
27   }

28   myGetResult.Calc(myGRdelegate);
29   Console.ReadLine();
30 }
 
31}

32//GGResult类
33 public class GGResult
34 {
35  long height;
36  long width;
37  long length;
38  public GGResult(long height,long width,long length)
39  {
40   this.height=height;
41   this.width=width;
42   this.length=length;
43  }

44  public delegate double MyDelegate(long length,long width);//声明委托
45  public void Calc(MyDelegate pmydelegate)
46  {
47   double volume;
48   volume=pmydelegate(length,width)*height;
49   Console.WriteLine("高度为{0}则体积为{1}",height,volume);
50  }

51 }

52 //Measure类
53 public class Measure
54 {
55  public double MeasureSquare(long length,long width)
56  {
57   double squareLength;
58   double squareSurface;
59   squareLength=4*length;
60   squareSurface=Math.Pow(length,2);
61   Console.WriteLine("长为{0}宽为{1}的正方的周长为{2},面积为{3}",length,width,squareLength,squareSurface);
62   return squareSurface;
63  }

64  public double MeasureRectangle(long length,long width)
65  {
66   double RectangleLength;
67   double RectangleSurface;
68   RectangleLength=2*(length+width);
69   RectangleSurface=length*width;
70   Console.WriteLine("长为{0}宽为{1}的长方形的周长为{2},面结为{3}:",length,width,RectangleLength,RectangleSurface);
71   return RectangleSurface;
72  }

73 }

74}

委派相当于一个容器,而这个容器使用来存放函数的或者说是用来封装函数的,而这个容器又可以看成一个函数,这个函数没有
   实现方法,只需要返回类型和参数与姚封装的函数一致就可以了,那么就可以封装这个函数了。
   例如上面的例子:
   1,声明了一个double型的委派myDelegate,参数为(long length,long width);那么此委派只能封装就有返回值且是double,而且
   参数为long的函数。
   2,在Measure类中,有两个方法,MeasureSquare和MeasureRectangle,这两个函数都具有反回值是double型的,参数也是long
   型的。所以这两个函数都可以分装到委派里面。
   3,上面说到,委派相当于一个函数,既然是函数,那么也就可以有参数和返回值(当然了要和所封装的函数一致)。
      1,在GGResult类中有一个方法Calc;它的参数为委派类型的参数,实际上是调用了委派所封装的方法,根据条件来初始化
      myDelegate所封装的方法,然后把这个方法通过参数的形式传送到Cale中,然后再Calc中调用这个方法。
   大概一看此程序没有什么可看的,其实不然,简单的看一下似乎没有问题,但仔细一想问题就出来了
   myDelegate=new GGResult.MyDelegate(myMeasure.MeasureSquare);
   myDelegate=new GGResult.MyDelegate(myMeasure.MeasureRectangle);
   这两句是初始化了委派即说明了委派来封装那个函数,并不是执行语句。那么什么时候开始执行这个委派呢?
   在 myGetResult.Calc(myGRdelegate);这句中,因为在Calc中有这么一句
   volume=pmydelegate(length,width)*height;
   所以在执行pmydelegate时就开始执行了上面的其中一个委派中的方法。
   那么参数从何而来?答案就是:在初始化GGResult时就把参数初始化到GGResult的构造函数中了,然后调用pmydelegate时
   就直接调用了。
  
     
   注意:在类中声明函数时,如果函数没有static关键字时,那么可以通过类的实例来引用,如果有static关键字时,只能用
   类名直接引用。
   类中类的初始化:
   在GGResult中声明了一个委派类MyDelegate那么在初始化时必须加上GGResult
   既GGResult.MyDelegate   实例名字=new GGResult.MyDelegate();

抱歉!评论已关闭.