在C#示例中调用基类

时间:2012-07-06 17:06:30

标签: c# inheritance constructor

请考虑以下代码:

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发送到构造函数Analyzer(Func f),有没有办法在这里做到这一点?尝试了类似的事情:

public PolyAnalyzer( params double[] coef ) : base(new Func(poly)){
            coefs = coef;
        }

但它没有编译...编译错误:: 非静态字段,方法或属性“成员”

需要对象引用

我赞成一个解释得很好的答案,而不仅仅是如何完成......:)

2 个答案:

答案 0 :(得分:1)

您收到的错误:

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

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

通常你可以通过添加object reference之类的方式解决这个问题(例如new Func(this.poly))但是当前的类实例(this)在上下文中不可用。 (你的实例尚未完全实现)。

您可以像在PolyAnalyzer构造函数中已经完成的那样,自己设置值,这是我的建议。

答案 1 :(得分: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;
            });
    }
}