利用委托,将变化的生产方法进行委托
1.定义委托
public delegate double Cal(double x,double y);
2.实例化委托
double Add(double x,double y){
return x+y;
}
double Dec(double x,double y){
return x-y;
}
//写法一
Cal cal = new Cal(Add);
//写法二
Cal cal = Add;
3.使用委托
void Test(Cal f)
{
Console.Write("请输入X:");
double x = Convert.ToDouble(Console.ReadLine());
Console.Write("请输入Y:");
double y = Convert.ToDouble(Console.ReadLine());
//委托方法工作,怎么工作由具体委托决定
double result = f(x,y);
Console.WriteLine(result);
}