此代码应该输出线,说得到一个很奇怪的逻辑错误,我不明白
TREX(2条腿)恐龙(灭绝)
对于来自文件动物名单。 如果动物没有灭绝,它会被if语句忽略。
一切工作正常,除了最后一点,应该说(灭绝)。
取而代之,它表示“真”或“假”,这取决于我如何设置isExtinct属性。
现在我已经把一个if语句到isExtinct属性输出 “灭绝”,但我得到一个错误后编译
Stacktrace:
Native stacktrace:
mono() [0x8105b4a]
mono() [0x8153274]
mono() [0x806a20b]
[0x777b940c]
mono() [0x8153224]
mono() [0x806a20b]
[0x777b940c]
[0x7744c667]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
Debug info from gdb:
=================================================================
Got a SIGSEGV while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries
used by your application.
=================================================================
Aborted (core dumped)
代码如下
class MainClass
{
public static void Main (string[] args)
{
StreamReader input = new StreamReader ("animals.txt");
while (!input.EndOfStream) {
string line = input.ReadLine();
string[] data = line.Split (',');
if (data [3] == "true") {
Animal myAnimal = new Animal (data[0], Convert.ToInt32(data[1]), data[2], Convert.ToBoolean(data[3]));
Console.WriteLine (myAnimal);
}
}
}
}
}
public class Animal
{
string species;
int legs;
string genus;
bool extinct;
public Animal (string species, int legs, string genus, bool extinct)
{
this.species = species;
this.legs = legs;
this.genus = genus;
this.extinct = extinct;
}
public string Species{ get; set;}
public int Legs{ get; set;}
public string Genus{ get; set;}
public string isExtinct{
get
{
return isExtinct;
}
set
{
if (extinct == true) {
isExtinct = "extinct";
} else {
isExtinct = "";
}
}
}
public override string ToString(){
return String.Format("{0} ({1} legged) {2} ({3})", species, legs, genus, isExtinct);
}
}
}
第一猜测是isextinct肯定你只需要一个getter。”如果空 – BugFinder
你得到一个'StackOverflowException '因为'isExtinct'的getter返回'isExtinct'(相同的属性)导致无限递归。 –