C# 程序不会 运行 过去的对象实例

C# program won't run past object instance

我正在创建一个程序,它应该创建一个带有音符泛音的列表并将它们显示在控制台上。该程序运行时没有编译器错误,但输出不是预期的,我似乎不明白为什么。稍后我会添加其他功能,但首先我想完成这项工作。主要方法如下所示:

    public static void Main (string[] args)
{
    //test method before object instance
    Console.WriteLine ("Test 1");

    //creates object instance with frequency = 110f
    CompositeWave myWave = new CompositeWave (110f);

    //test method after object instance
    Console.WriteLine ("Test 2");

    //CompositeWave method that adds items to overtoneWavesList
    myWave.createNaturalOvertones();

    //prints all overtone frequencies in order
    foreach (KeyValuePair<byte,SineWave> valuePair in myWave.overtoneWavesList) {

        Console.WriteLine (valuePair.Key + " - " + valuePair.Value.tonicFrequecy);
    }

}

预期的输出应该类似于

        /*
     * Test 1
     * Test 2
     * 0 - 220
     * 1 - 330
     * 2 - 440
     * 3 - 550
     * ... so on
     * /

但输出只是

        /*
     * Test 1
     * /

并且该计划永远不会完成,也不会再进一步​​。

这些是我在命名空间中使用的 classes(它们仍然具有未实现的功能,例如波的振幅):

声波的基本class(频率不能高于24K,因为那样就听不见了):

    /// <summary>
/// Basic sound wave class.
/// </summary>
public abstract class Wave
{
    //simple wave properties
    public float tonicFrequecy 
    {
        get 
        {
            return tonicFrequecy;
        }
        set
        {
            if (value > 24000f) {
                tonicFrequecy = 24000f;
            } else {
                tonicFrequecy = value;
            }
        }
    }
    public float tonicAmplitude { get; set;}
}

从基波class导出的正弦波class:

    /// <summary>
/// Simple sine wave with frequency spectrum concentred in only one fundamental frequency.
/// </summary>
public class SineWave : Wave
{
    //initializer
    public SineWave (float frequency = 440f, float amplitude = 0f)
    {
        this.tonicFrequecy = frequency;
        this.tonicAmplitude = amplitude;
    }
}

以及从基波 class 派生的复合波 class(我在 Main 方法中实例化):

    /// <summary>
/// Complex sound wave composed of multiple sine waves with different frequencies and amplitudes.
/// </summary>
public class CompositeWave : Wave
{
    //initializer
    public CompositeWave (float frequency = 440f, float amplitude = 0f)
    {
        this.tonicFrequecy = frequency;
        this.tonicAmplitude = amplitude;

        this.overtoneWavesList = new SortedList<byte, SineWave>(1);
    }

    //overtone list that compose the composite wave
    public SortedList<byte,SineWave> overtoneWavesList { get; set;}

    /// <summary>
    /// Creates possible natural overtones and sets all amplitudes to zero.
    /// </summary>
    public void createNaturalOvertones ()
    {
        float overtoneFrequency = 0f;

        for (byte i = 2; overtoneFrequency <= 24000f; i++) {

            //sets overtone frequency as multiple of fundamental
            overtoneFrequency = this.tonicFrequecy * (float)i;
            //sets list key and add overtone to list
            int key = (int)i - 2;
            this.overtoneWavesList.Add ((byte)key, new SineWave(overtoneFrequency));
        }
    }
}

我已经尝试了几乎所有的方法,但不明白为什么它没有比测试 1 更进一步:( 我知道这是一个很长 post 的简单问题,对此我深表歉意,但在此先感谢您的任何答复!

您可能在构建 CompositeWave 实例期间遇到 Whosebug 异常,因为您的 tonicFrequency 属性 是自引用的:

public float tonicFrequecy 
{
    get 
    {
        return tonicFrequecy;  // this points back to itself
    }

A 属性 是对 class 数据的抽象 - 它不为 class 数据提供实际的 存储 。您需要在 class 中有一个单独的字段用于存储。例如:

private float _tonicFrequency; // this private field stores the data

// the property uses the private field for storage
public float tonicFrequency {
  get { return _tonicFrequency; }
  set { if (value > 24000f) _tonicFrequency = 24000f; else _tonicFrequency = value; }
}

auto-properties 的情况下(就像你的 tonicAmplitude),你没有这个问题,因为编译器会自动为你添加一个内部字段——但肯定有一个单独的私有字段字段支持 属性.

tonicFrequency 的 set 实现正在为自身设置一个值,因此程序进入无限循环。

您必须为 Wave class 编写如下代码。

public abstract class Wave
{
    private float _tonicFrequency;

    //simple wave properties
    public float tonicFrequecy
    {
        get { return _tonicFrequency; }
        set
        {
            if (value > 24000f)
            {
                _tonicFrequency = 24000f;
            }
            else
            {
                _tonicFrequency = value;
            }
        }
    }

    public float tonicAmplitude { get; set; }
}