gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 15:24:57 25 4
gpt4 key购买 nike

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

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

最佳答案

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

关于C# 程序不会运行过去的对象实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31978418/

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