WHYYYYYYY?
以下是我的一半代码,因为我已经删除了大部分代码以及我在尝试解决此问题时丢失的每一根头发。无论我删除或更改什么,我都会收到StackOverflowException。
这是非常奇怪的,因为这个完全相同的代码更早。
请给我任何建议,因为我很无能......
我已经检查了这一点,我认为这不是发生的事情:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net;
namespace ConsoleApplication10
{
class Program
{
public int currentDialogueID = 0;
List<NPC> NPCList = new List<NPC>() {
new NPC(0, "Man", 1, true),
new NPC(1, "Woman", 1, true),
new NPC(2, "Troll", 3, true)
};
static void Main(string[] args)
{
Program d = new Program();
d.Init();
Console.ReadKey(false);
}
void Init()
{
getNPCByName("Man").NPCDialogue();
}
public NPC getNPCByName(string npcName)
{
IEnumerable<NPC> myNPCsName = from nn in NPCList
where nn._name.ToLower() == npcName.ToLower()
orderby nn._name ascending
select nn;
foreach(NPC nn2 in myNPCsName)
{
return nn2;
}
return null;
}
public NPC getNPCByID(int npcID)
{
IEnumerable<NPC> NPCsByID = from ni in NPCList
where ni._npcID == npcID
orderby ni._npcID ascending
select ni;
foreach(NPC ni2 in NPCsByID)
{
return ni2;
}
return null;
}
public string getNPCNameByID(int npcid)
{
return getNPCByID(npcid)._name;
}
}
class NPC : Program
{
public string _name = "null";
public int _level;
public int _npcID;
public int _maxDamage;
public bool _canFight = false;
public NPC(int npcID = 0, string name = "null", int level = 0, bool canSpeak = false, bool canFight = false, int maxDamage = 0)
{
_level = level;
_name = name;
_npcID = npcID;
_canFight = canFight;
_maxDamage = maxDamage;
}
public void NPCDialogue()
{
currentDialogueID = _npcID;
switch(_npcID)
{
case 0:
NPCSpeak("Man test... ... ...");
break;
case 1:
NPCSpeak("Woman test");
break;
case 2:
NPCSpeak("I'm Elad the Troll, Ramzes your ear is that of an elf");
break;
default:
return;
}
}
public void NPCSpeak(string text, int npcID = 99999)
{
if(npcID == 99999)
npcID = currentDialogueID;
if(npcID != 99999)
type(getNPCNameByID(npcID) + ": " + text);
}
public void type(string x)
{
Random rnd = new Random();
char[] xx = x.ToCharArray();
for(int i = 0; i < xx.Length; i++)
{
Console.Write(xx[i]);
System.Threading.Thread.Sleep(rnd.Next(10, 120));
if(xx[i] == ':' || (xx[i] == '.' && xx[i - 1] != '.' && xx[i + 1] != '.') || xx[i] == '!' || xx[i] == '\n' || xx[i] == '?')
{
System.Threading.Thread.Sleep(rnd.Next(400, 1500));
}
}
}
}
class Item : Program
{
public string _name = "null";
public string _description = "How did you get this?";
public bool _isWeapon = false;
public int _maxDamage;
public int _itemID = 0;
public Item(int itemID = 0, string name = "null", string description = "null", bool isWeapon = false, int maxDamage = 0)
{
_itemID = itemID;
_name = name;
_description = description;
_isWeapon = isWeapon;
_maxDamage = maxDamage;
}
}
}
答案 0 :(得分:5)
你有NPC是一个程序,但是每个程序都有一个3 NPC的列表......你看到了吗?
下次,您查看“调用堆栈”窗口,您将看到
ConsoleApplication5.exe!ConsoleApplication10.Program.Program() Line 13 + 0xffffffe6 bytes C#
ConsoleApplication5.exe!ConsoleApplication10.NPC.NPC(int npcID, string name, int level, bool canSpeak, bool canFight, int maxDamage) Line 77 + 0x8 bytes C#
ConsoleApplication5.exe!ConsoleApplication10.Program.Program() Line 15 + 0x40 bytes C#
ConsoleApplication5.exe!ConsoleApplication10.NPC.NPC(int npcID, string name, int level, bool canSpeak, bool canFight, int maxDamage) Line 77 + 0x8 bytes C#
ConsoleApplication5.exe!ConsoleApplication10.Program.Program() Line 15 + 0x40 bytes C#
ConsoleApplication5.exe!ConsoleApplication10.NPC.NPC(int npcID, string name, int level, bool canSpeak, bool canFight, int maxDamage) Line 77 + 0x8 bytes C#
ConsoleApplication5.exe!ConsoleApplication10.Program.Program() Line 15 + 0x40 bytes C#
ConsoleApplication5.exe!ConsoleApplication10.NPC.NPC(int npcID, string name, int level, bool canSpeak, bool canFight, int maxDamage) Line 77 + 0x8 bytes C#
ConsoleApplication5.exe!ConsoleApplication10.Program.Program() Line 15 + 0x40 bytes C#
然后您可以单击这些行并查看下一步的启动位置。
答案 1 :(得分:1)
看起来你对哪些类应该继承的地方有点困惑。
您的程序类会创建一个包含3个NPC的新列表。 NPC继承自Program,这意味着(基本上)程序中的所有代码都被放入NPC中。这意味着,由于程序在实例化时创建了3个NPC的列表,NPC继承了这个代码并创建了另外3个NPC的列表,每个NPC都从程序继承,每个NPC再创建3个NPC,直到堆栈为止溢出。
关于查看调用堆栈以获取此内容的提示是一个很好的建议。我还要补充说,继承通常使用略有不同。仅仅因为您的程序使用NPC和Item并不意味着他们必须(或应该)继承它。继承最好在类之间使用,其中一个是另一个的子集,但具有更多特定的特征。一般的例子是具有属性'Family,Species,Age'等的Animal类可能由类Dog继承,它也具有所有这些属性,但您也可能想要使用Dog特定属性'Breed'。
在你的情况下,NPC可能是由“Merchant”或“Companion”继承的父类,这两个类仍然是NPC(因此应该具有所有基本的NPC特征),但也可能具有不同的特定于他们的行为子类。物品可能会被“武器”和“盾牌”继承,它们都是物品,但具有不同的附加特征以便跟踪。
答案 2 :(得分:-2)
Program d = new Program();
你必须删除这一行,每次运行程序构造函数时都要调用程序构造函数,导致stackoverflow