我正在创建一个程序,它应该创建一个带有音符暗示的列表,并在控制台上显示它们。程序运行时没有编译器错误,但是输出不是预期的,我似乎不明白为什么。稍后我会添加其他特性,但首先我想让这个功能发挥作用。主要方法如下所示:
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;
}
}以及从基波类ass中导出的复合波类(我正在主要方法中实例化):
/// <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更深入:(我知道这是一个很长的帖子来回答一个简单的问题,对此我很抱歉,但是提前感谢你的回答!)
发布于 2015-08-13 02:37:39
您可能在构造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一样),您没有这个问题,因为编译器会自动为您添加一个内部字段--但是肯定有一个单独的私有字段支持该属性。
发布于 2015-08-13 03:00:17
您必须为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; }
}发布于 2015-08-13 02:43:56
tonicFrequency的set实现正在为自己设置一个值,因此程序进入一个无限循环。
https://stackoverflow.com/questions/31978418
复制相似问题