internal class ST
{
internal void optimize(int m,int n,int f)
{
int om, on;
for (int i = m; i <= m + f; ++i)
{
for (int j = n; j <= n + f; ++i)
{
on = j;
run();
}
om = i;
}
}
internal virtual void run()
{ }
}
internal class STA:ST
{
internal override void run(float a,float b,int m=16,int n=35)
{
}
}
—
STA sta=new STA;
sta.optimize(10,10,20);//这个要怎么写才能实现optimize()方法内部调用子类中带参数的run()方法呢?
{
internal void optimize(int m,int n,int f)
{
int om, on;
for (int i = m; i <= m + f; ++i)
{
for (int j = n; j <= n + f; ++i)
{
on = j;
run();
}
om = i;
}
}
internal virtual void run()
{ }
}
internal class STA:ST
{
internal override void run(float a,float b,int m=16,int n=35)
{
}
}
—
STA sta=new STA;
sta.optimize(10,10,20);//这个要怎么写才能实现optimize()方法内部调用子类中带参数的run()方法呢?
解决方案
40
这样不行吧,可以尝试这样:
internal class ST { internal void optimize(int m, int n, int f, Delegate dele, params object[] parameters) { int om, on; for (int i = m; i <= m + f; ++i) { for (int j = n; j <= n + f; ++i) { on = j; dele.Method.Invoke(this, parameters);//run(); } om = i; } } } internal class STA : ST { internal void run(float a, float b, int m = 16, int n = 35) { } }
STA sta = new STA(); sta.optimize(10, 10, 20, new Action<float, float, int, int>(sta.run), 1, 2, 3, 4);