用C#委托和事件编程
内容:用事件和委托实现两个整数的加法、减法和求最大值。要求:定义事件触发对两个整数的操作,并使用多重委托来调用加法、减法和求最大值。
追问:main 函数, 要带输入输出的 ,谢谢!
内容:用事件和委托实现两个整数的加法、减法和求最大值。要求:定义事件触发对两个整数的操作,并使用多重委托来调用加法、减法和求最大值。
追问:main 函数, 要带输入输出的 ,谢谢!
答案:class Test {
static void Main()
{
new Test().OnOperation(1, 2);
}
public delegate void operation(int a, int b);
public event operation op;
public Test()
{
op += new operation(t_Max);
op += new operation(t_Add);
op += new operation(t_Sub);
}
public void OnOperation(int a, int b)
{
if (op != null)
op(a, b);
}
void t_Max(int a, int b)
{
Console.WriteLine("Max:{0}", a > b ? a : b);
}
void t_Add(int a, int b)
{
Console.WriteLine("a+b:{0}", a + b);
}
void t_Sub(int a, int b)
{
Console.WriteLine("a-b:{0}", a-b);
}
}
上一个:编程新手怎么速学c#?
下一个:C#操作题编程题