编写关于多点委托应用的实例

2016-06-11  本文已影响0人  目标肢解

多点委托

MyDelegate d = new MyDelegate(MyClass.Square);

d += new MyDelegate(MyClass.Cube);

d += new MyDelegate(MyClass.Double);

ExecuteMethod(d, 2);


static void ExecuteMethod(MyDelegate d, float x)

{

d(x);

}

delegate void MyDelegate(float x);


class MyClass

{

public static void Square(float x)

{

float result = x * x;

Console.WriteLine("{0}的平方等于:{1}", x, result);

}

public static void Cube(float x)

{

float result = x * x * x;

Console.WriteLine("{0}的立方等于:{1}", x, result);

}

public static void Double(float x)

{

float result = 2 * x;

Console.WriteLine("{0}的倍数等于:{1}", x, result);

}

}

上一篇下一篇

猜你喜欢

热点阅读