0

考虑下面的代码:

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 一个很好解释的答案,而不仅仅是它是如何完成的...... :)

4

2 回答 2

1

你得到的错误:

An object reference is required for the non-static field, method, or property...

这是由于 poly 是您正在创建的对象的成员,它取决于PolyAnalyzer类中的变量。

通常,您可以通过添加一个object reference类似它调用的 (ie new Func(this.poly)) 来解决此问题,但是当前类 ( this) 的实例在上下文中尚不可用。(您的实例尚未完全实现)。

您可以像在PolyAnalyzer构造函数中所做的那样,自己设置值,这将是我的建议。

于 2012-07-06T17:26:44.720 回答
0

在我看来,您正在尝试将面向对象的继承和函数式编程结合起来,但在这种情况下效果不佳。

我会写

public abstract class Analyzer {
    protected abstract double Fd(double x);
    // ...
}

并在后代类中覆盖它(如果您想坚持使用 OO 层次结构)。这是经典的策略模式实现。

要解决您的评论,如果您希望 Analyzer 可实例化,请使用:

public class Analyzer {
    protected virtual double Fd(double x)
    {
        // provide default implementation
    }
    // ...
}

如果你想坚持函数式编程,我会使用组合而不是继承:

// Does not descend from Analyzer. Could implement IAnalyzer.
public class PolyAnalyzer {
    private readonly Analyzer analyzer;
    private double[] coefs;

    public PolyAnalyzer( params double[] coef ) {
        coefs = coef;

        analyzer = new Analyzer(poly);
    }

    public double evaluate(double x) {
        return analyzer.evaluate(x);
    }

    // Implement evaluateDerived and solve through delegation

    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;
    }
}

或者,如果您接受@Reed Copsey 的建议并切换到Func<double, double>,您可以在工厂方法中使用闭包:

public static class PolyAnalyzerFactory {
    public static Analyzer Create( params double[] coef ) {
        var coefs = coef.ToArray();  // protect against mutations to original array

        return new Analyzer(
            x =>
            {
                double sum = 0;
                for ( int i = 0 ; i < coefs.Length ; i++ ) {
                    sum += coefs[i] * Math.Pow(x,coefs.Length-1-i);
                }
                return sum;
            });
    }
}
于 2012-07-06T17:30:00.553 回答