<分区>
我写了一些c#代码,实现了几个函数的传递。我为此使用了委托(delegate)。
public delegate float Func(float x);
public Dictionary<string, Func> Functions = new Dictionary<string, Func>();
填写字典:
Functions.Add("y=x", delegate(float x) { return x; });
Functions.Add("y=x^2", delegate(float x) { return x*x; });
Functions.Add("y=x^3", delegate(float x) { return x*x*x; });
Functions.Add("y=sin(x)", delegate(float x) { return (float)Math.Sin((double)x); });
Functions.Add("y=cos(x)", delegate(float x) { return (float)Math.Cos((double)x); });
我使用这些函数来查找它们的最大值和一些其他任务。
private float maxY(params Func[] F)
{
float maxYi = 0;
float maxY = 0;
foreach (Func f in F)
{
for (float i = leftx; i < rightx; i += step)
{
maxYi = Math.Max(f(i), maxYi);
}
maxY = Math.Max(maxY, maxYi);
}
return maxY;
}
我如何在 Java 中执行此操作?
我不知道如何在 Java 中使用委托(delegate)。