c#程序不能运行过去的对象实例

本文关键字:对象 实例 过去 运行 程序 不能 | 更新日期: 2023-09-27 18:07:44

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

    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
     * /

,程序永远不会结束,也不会继续运行。

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

声波的基本类别(频率不能高于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;}
}

从基波类派生的正弦波类:

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

和一个复合波类(我在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更进一步。我知道这是一个很长的帖子,一个简单的问题,我很抱歉,但一个很大的感谢提前任何回应!

c#程序不能运行过去的对象实例

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

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

你可能会得到一个StackOverflow异常在你的CompositeWave实例的建设,因为你的tonicFrequency属性是自引用:

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

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

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

在自动属性的情况下(比如你的tonicAmplitude),你没有这个问题,因为编译器会自动为你添加一个内部字段——但是肯定有一个单独的私有字段支持该属性。

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