我有一个播放器类,我试图将该类引用到我的主类。但我在里面放了一些代码,当我试图引用它到我的主类时,它会给我一个错误。没有给定的参数对应于所需的正式周长,即"玩家"的"statName"。有人能帮我吗?我很确定它与所有的代码混在一起,但我只是不知道,我想要一点帮助。这是我的课。
这是我的程序类
class Program
{
PLayer Playerstat = new PLayer(); // I'm trying to reference the class Player here but it's not letting me.
static void Main()
{
Console.WriteLine("please pick a mode");
PLayGame();
PickMode();
Console.ReadKey();
}
public static void PLayGame()
{
List<string> Options = new List<string> { "Easy mode: ", "med mode: ", "Hard mode", "super hard mode: " };
foreach(string ModePick in Options)
{
Thread.Sleep(570);
Console.WriteLine(ModePick);
}
}
private static void PickMode()
{
string pick = Console.ReadLine();
if (pick == "easy")
{
Console.WriteLine("you have picked easy.");
}
}
}
这是我的播放器类
public class PLayer
{
public string Name;
private int _statValue;
private string _statName;
public bool Dead;
public PLayer(string statName, int statValue)
{
_statName = statName;
_statValue = statValue;
}
public override string ToString() => _statName + ": " + _statValue;
List<PLayer> stats = new List<PLayer>
{ new("Health", 100),
new("Accuracy", 89),
new("Blade Amount", 2),
new("Blade Damage", 20),
new("Blade Lanth", 40),
new("Blade durablity", 50),
new("Col", 0)
};
//all the NPCs you know.
List<string> Npc = new List<string> { "levi: ", "Armin: ", "Mikasa: ", "Hange: ", "Erwin: " };
//skills for killing titans
List<string> Skills = new List<string>();
public void PrintStats()
{
Console.WriteLine("your stats are: ");
foreach (var stat in stats)
{
Thread.Sleep(575);
Console.WriteLine(stat.ToString());
}
}
}
-
像"PLayer"(包括"P"one_answers";L">
最佳实践:使用PascalCase,例如:class Player
-
使用名称空间。MSVS将默认为您完成此操作(如果您使用的是Visual Studio IDE);你没有告诉我们你自己是否在使用它们。
当引用不同模块中的对象时,使用名称空间可以很容易地限定类名。
-
主要问题:
如果你想要
Player Playerstat = new Player()
…那么你需要为"player"声明一个无参数(parameterless")构造函数。
你的代码有几个问题,一个巨大的一个是堆栈溢出错误.
你的PLayer
类的设计方式会导致一个无限循环,如果你一开始就正确地创建它的话。
PLayer
类
在你的PLayer
类你有一个构造函数:
public PLayer(string statName, int statValue)
{
_statName = statName;
_statValue = statValue;
}
它要求您在实例化PLayer
时提供string
statName
和int
statValue
。
那么在你的Program
中,你写的那行是:
PLayer Playerstat = new PLayer(); // I'm trying to reference the class Player here but it's not letting me.
需要修改为:
PLayer Playerstat = new PLayer("ExampleString", 1); // I'm trying to reference the class Player here but it's not letting me.
然而,这不是这里的主要问题。如果您最终修复了这个问题,您将运行程序并遇到堆栈溢出错误。原因是,你的PLayer
类,在实例化时,试图在自己的代码中创建一个List<PLayer>
:
List<PLayer> stats = new List<PLayer>
{ new("Health", 100),
new("Accuracy", 89),
new("Blade Amount", 2),
new("Blade Damage", 20),
new("Blade Lanth", 40),
new("Blade durablity", 50),
new("Col", 0)
};
您尝试在这里创建的每个PLayer
也将尝试创建自己的PLayers
列表,该列表无限重复,直到您的计算机崩溃。
如果您希望能够创建具有可打印的自定义统计信息的PLayer
,也许可以尝试在保存统计信息的PLayer
类中使用Dictionary
接近它。这样,您就可以向PLayer
构造函数提供Dictionary
,以便在实例化时填充它。
也许像下面这样的PLayer
类更适合您的需要:
public class PLayer
{
public string Name;
public bool Dead;
public PLayer(string name = "", Dictionary<string, int> stats = null)
{
Name = name;
if (stats != null)
this.Stats = stats;
}
Dictionary<string, int> Stats = new Dictionary<string, int>
{
{ "Health", 100 },
{ "Accuracy", 89 },
{ "Blade Amount", 2},
{ "Blade Damage", 20 },
{ "Blade Lanth", 40 },
{ "Blade durablity", 50 },
{ "Col", 0}
};
//all the NPCs you know.
List<string> Npc = new List<string> { "levi: ", "Armin: ", "Mikasa: ", "Hange: ", "Erwin: " };
//skills for killing titans
List<string> Skills = new List<string>();
public void PrintStats()
{
Console.WriteLine("your stats are: ");
foreach (var stat in stats)
{
Thread.Sleep(575);
Console.WriteLine(stat.ToString());
}
}
}
你可以这样写:
class Program
{
static PLayer Playerstat;
static void Main()
{
Dictionary<string, int> CustomStats = new Dictionary<string, int>
{
{ "Health", 1000 },
{ "Accuracy", 5 },
{ "Blade Amount", 15},
{ "Blade Damage", 5 },
{ "Blade Lanth", 20 },
{ "Blade durablity", 100 },
{ "Col", 0}
};
// Here we actually provide parameters for the constructor
Playerstat = new PLayer("Example Player Name", CustomStats);
Console.WriteLine("please pick a mode");
PLayGame();
PickMode();
Console.ReadKey();
}
public static void PLayGame()
{
List<string> Options = new List<string> { "Easy mode: ", "med mode: ", "Hard mode", "super hard mode: " };
foreach(string ModePick in Options)
{
Thread.Sleep(570);
Console.WriteLine(ModePick);
}
}
private static void PickMode()
{
string pick = Console.ReadLine();
if (pick == "easy")
{
Console.WriteLine("you have picked easy.");
}
}
}