考虑下面的代码:
public class Analyzer {
protected Func f,fd;
public delegate double Func( double x );
public Analyzer( Func f, Func fd ) {
this.f = f;
this.fd = fd;
}
public Analyzer( Func f ) {
this.f = f;
fd = dx;
}
public Analyzer( ) { }
protected double dx( double x ) {
double h = x / 50.0;
return ((f(x + h) - f(x - h)) / (2 * h));
}
public double evaluate(double x) {
return f( x );
}
public double evaluateDerived( double x ) {
return fd( x );
}
public double solve(double x0) {
double eps = 1, x1 = f(x0), x2 = fd(x0);
do x0 = x0 - ( f( x0 ) / fd( x0 ) );
while ( f( x0 ) > eps );
return x0;
}
}
public class PolyAnalyzer : Analyzer {
private double[] coefs;
public PolyAnalyzer( params double[] coef ) {
coefs = coef;
f = poly;
fd = dx;
}
private double poly( double x ) {
double sum = 0;
for ( int i = 0 ; i < coefs.Length ; i++ ) {
sum += coefs[i] * Math.Pow(x,coefs.Length-1-i);
}
return sum;
}
}
我试图想办法将 poly 发送到构造函数 Analyser(Func f),这里有没有办法做到这一点?尝试过类似的东西:
public PolyAnalyzer( params double[] coef ) : base(new Func(poly)){
coefs = coef;
}
但它不编译...编译错误:: 非静态字段、方法或属性“成员”需要对象引用
Id appriciate 一个很好解释的答案,而不仅仅是它是如何完成的...... :)