如何重构这些重载的类构造函数? (C#)

时间:2009-07-16 01:41:13

标签: c# .net refactoring

我不想在重载的构造函数中复制代码,如下所示。我该如何重构呢?

/// <summary>
/// Represents a pseudo-random number generator, a device that
/// produces a sequence of numbers that are normally distributed.
/// </summary>
public class NormalDeviate : UniformDeviate
{
    double mean;
    double standardDeviation;
    double storedValue = 0d;

    /// <summary>
    /// Initializes a new instance of the <see cref="NormalDeviate"/> class.
    /// </summary>
    /// <param name="mean">The mean.</param>
    /// <param name="standardDeviation">The standard deviation.</param>
    /// <param name="seed">The seed.</param>
    public NormalDeviate(double mean, double standardDeviation, ulong seed)
        : base(seed)
    {
        this.mean = mean;
        this.standardDeviation = standardDeviation;
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="NormalDeviate"/> class.
    /// </summary>
    /// <param name="mean">The mean.</param>
    /// <param name="standardDeviation">The standard deviation.</param>
    public NormalDeviate(double mean, double standardDeviation)
        : base()
    {
        this.mean = mean;
        this.standardDeviation = standardDeviation;
    }
}

3 个答案:

答案 0 :(得分:2)

鉴于在没有提供种子时你无法为基础构造函数提供0种子,这就是你想要的:

/// <summary>
/// Represents a pseudo-random number generator, a device that
/// produces a sequence of numbers that are normally distributed.
/// </summary>
public class NormalDeviate : UniformDeviate
{
    double mean;
    double standardDeviation;
    double storedValue = 0d;

    /// <summary>
    /// Initializes a new instance of the <see cref="NormalDeviate"/> class.
    /// </summary>
    /// <param name="mean">The mean.</param>
    /// <param name="standardDeviation">The standard deviation.</param>
    /// <param name="seed">The seed.</param>
    public NormalDeviate(double mean, double standardDeviation, ulong seed)
        : base(seed)
    {
        CommonInitialization(mean, standardDeviation);
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="NormalDeviate"/> class.
    /// </summary>
    /// <param name="mean">The mean.</param>
    /// <param name="standardDeviation">The standard deviation.</param>
    public NormalDeviate(double mean, double standardDeviation)
        : base()
    {
        CommonInitialization(mean, standardDeviation);
    }

    private void CommonInitialization(double mean, double standardDeviation)
    {
        this.mean = mean;
        this.standardDeviation = standardDeviation;
    }
}

答案 1 :(得分:1)

public ClassName(double arg1, double arg2, double arg3) : base(arg3) 
{
    _member1 = arg1;
    _member2 = arg2;
}
public ClassName(double arg1, double arg2) : this(arg1,arg2,0) { }
public ClassName(double arg1) : this(arg1,0,0) { }

答案 2 :(得分:0)

这个怎么样?

public class NormalDeviate : UniformDeviate
{
    double mean;
    double standardDeviation;
    double storedValue;

    /// <summary>
    /// Initializes a new instance of the <see cref="NormalDeviate"/> class.
    /// </summary>
    /// <param name="mean">The mean.</param>
    /// <param name="standardDeviation">The standard deviation. The distance from the mean where 78.2% of the samples lie.</param>
    /// <param name="seed">The seed.</param>
    public NormalDeviate(double mean, double standardDeviation, ulong seed) : base(seed)
    {
        this.mean = mean;
        this.standardDeviation = standardDeviation;
        this.storedValue = 0d;
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="NormalDeviate"/> class.
    /// </summary>
    /// <param name="mean">The mean.</param>
    /// <param name="standardDeviation">The standard deviation. The distance from the mean where 78.2% of the samples lie.</param>
    public NormalDeviate(double mean, double standardDeviation)
        : this(mean, standardDeviation, 0)
    {

    }
}

编辑:修正代码