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

时间:2015-08-13 02:13:26

标签: c# object console instance sortedlist

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

    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更进一步:( 我知道对于一个简单的问题,这是一个很长的帖子,对此我很抱歉,但是对任何回复都要提前表示非常感谢!

3 个答案:

答案 0 :(得分:2)

你必须为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; }
}

答案 1 :(得分:1)

您可能在构建CompositeWave实例期间收到StackOverflow异常,因为您的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; }
}

对于auto-properties(与您的tonicAmplitude一样),您没有此问题,因为编译器会自动为您添加内部字段 - 但肯定有一个单独的私有字段支持该财产。

答案 2 :(得分:1)

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