可以使用一个委派调用多个方法,委派和方法都有一个限制:委派和方法都必须返回void。
下面的例子定义了一个叫DelegateCalculation的返回值为void的派生类:
public delegate void DelegateCalculation(
double acceleration, double time
);
1
/*
2 Example12_2.cs illustrates the use of a multicast delegate
3 */
4
5 using System;
6
7
8 // declare the DelegateCalculation delegate class
9 public delegate void DelegateCalculation(
10 double acceleration, double time
11 );
12
13
14 // declare the MotionCalculations class
15 public class MotionCalculations
16 {
17
18 // FinalSpeed() calculates the final speed
19 public static void FinalSpeed(
20 double acceleration, double time
21 )
22 {
23 double finalSpeed = acceleration * time;
24 Console.WriteLine( " finalSpeed = " + finalSpeed +
25 " meters per second " );
26 }
27
28 // Distance() calculates the distance traveled
29 public static void Distance(
30 double acceleration, double time
31 )
32 {
33 double distance = acceleration * Math.Pow(time, 2 ) / 2 ;
34 Console.WriteLine( " distance = " + distance + " meters " );
35 }
36
37 }
38
39
40 class Example12_2
41 {
42
43 public static void Main()
44 {
45
46 // declare and initialize the acceleration and time
47 double acceleration = 10 ; // meters per second per second
48 double time = 5 ; // seconds
49 Console.WriteLine( " acceleration = " + acceleration +
50 " meters per second per second " );
51 Console.WriteLine( " time = " + time + " seconds " );
52
53 // create delegate object that call the
54 // MotionCalculations.FinalSpeed() and
55 // MotionCalculations.Distance() methods
56 DelegateCalculation myDelegateCalculation1 =
57 new DelegateCalculation(MotionCalculations.FinalSpeed);
58 DelegateCalculation myDelegateCalculation2 =
59 new DelegateCalculation(MotionCalculations.Distance);
60
61 // create a multicast delegate object from
62 // myDelegateCalculation1 and
63 // myDelegateCalculation2
64 DelegateCalculation myDelegateCalculations =
65 myDelegateCalculation1 + myDelegateCalculation2;
66
67 // calculate and display the final speed and distance
68 // using myDelegateCalculations
69 myDelegateCalculations(acceleration, time);
70
71 }
72
73 }
2 Example12_2.cs illustrates the use of a multicast delegate
3 */
4
5 using System;
6
7
8 // declare the DelegateCalculation delegate class
9 public delegate void DelegateCalculation(
10 double acceleration, double time
11 );
12
13
14 // declare the MotionCalculations class
15 public class MotionCalculations
16 {
17
18 // FinalSpeed() calculates the final speed
19 public static void FinalSpeed(
20 double acceleration, double time
21 )
22 {
23 double finalSpeed = acceleration * time;
24 Console.WriteLine( " finalSpeed = " + finalSpeed +
25 " meters per second " );
26 }
27
28 // Distance() calculates the distance traveled
29 public static void Distance(
30 double acceleration, double time
31 )
32 {
33 double distance = acceleration * Math.Pow(time, 2 ) / 2 ;
34 Console.WriteLine( " distance = " + distance + " meters " );
35 }
36
37 }
38
39
40 class Example12_2
41 {
42
43 public static void Main()
44 {
45
46 // declare and initialize the acceleration and time
47 double acceleration = 10 ; // meters per second per second
48 double time = 5 ; // seconds
49 Console.WriteLine( " acceleration = " + acceleration +
50 " meters per second per second " );
51 Console.WriteLine( " time = " + time + " seconds " );
52
53 // create delegate object that call the
54 // MotionCalculations.FinalSpeed() and
55 // MotionCalculations.Distance() methods
56 DelegateCalculation myDelegateCalculation1 =
57 new DelegateCalculation(MotionCalculations.FinalSpeed);
58 DelegateCalculation myDelegateCalculation2 =
59 new DelegateCalculation(MotionCalculations.Distance);
60
61 // create a multicast delegate object from
62 // myDelegateCalculation1 and
63 // myDelegateCalculation2
64 DelegateCalculation myDelegateCalculations =
65 myDelegateCalculation1 + myDelegateCalculation2;
66
67 // calculate and display the final speed and distance
68 // using myDelegateCalculations
69 myDelegateCalculations(acceleration, time);
70
71 }
72
73 }
接着,在MotionCalculations类中定义两个返回void的叫FinalSpeed和Distancede方法。
public
class
MotionCalculations
{
// FinalSpeed() calculates the final speed
public static void FinalSpeed(
double acceleration, double time
)
{
double finalSpeed = acceleration * time;
Console.WriteLine( " finalSpeed = " + finalSpeed +
" meters per second " );
}
// Distance() calculates the distance traveled
public static void Distance(
double acceleration, double time
)
{
double distance = acceleration * Math.Pow(time, 2 ) / 2 ;
Console.WriteLine( " distance = " + distance + " meters " );
}
}
{
// FinalSpeed() calculates the final speed
public static void FinalSpeed(
double acceleration, double time
)
{
double finalSpeed = acceleration * time;
Console.WriteLine( " finalSpeed = " + finalSpeed +
" meters per second " );
}
// Distance() calculates the distance traveled
public static void Distance(
double acceleration, double time
)
{
double distance = acceleration * Math.Pow(time, 2 ) / 2 ;
Console.WriteLine( " distance = " + distance + " meters " );
}
}
接着,创建两个委派对象,并把MotionCalculations.FinalSpeed和MotionCalculations.Distance传递给它的构建函数
DelegateCalculation myDelegateCalculation1
=
new DelegateCalculation(MotionCalculations.FinalSpeed);
DelegateCalculation myDelegateCalculation2 =
new DelegateCalculation(MotionCalculations.Distance);
new DelegateCalculation(MotionCalculations.FinalSpeed);
DelegateCalculation myDelegateCalculation2 =
new DelegateCalculation(MotionCalculations.Distance);
这两个委派可以使用重载的加运算符(+)来合在一起
myDelegateCalculations(acceleration, time);
全部代码为: