gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 22:49:26 24 4
gpt4 key购买 nike

我不想像下面这样跨重载构造函数复制代码。我该如何重构它?

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

最佳答案

鉴于您无法在未提供种子时向基本构造函数提供 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;
}
}

关于c# - 如何重构这些重载的类构造函数? (C#),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1135022/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com