namespace Tales_Of_Myroth
{
public class TalesOfMyroth
{
static void Main(string[] args)
{
TalesOfMyroth(); //here is my error
}
private Jogador _jogador;
public TalesOfMyroth()
{
_jogador = new Jogador();
_jogador.VidaAtual = 50;
_jogador.VidaMaxima = 50;
_jogador.Ouro = 0;
Console.WriteLine("Vida: " + _jogador.VidaAtual);
}
}
}
我原本打算为我的学院制作一个简单的文字游戏,我刚开始......有人可以帮我这个吗? (代码中的某些东西是用葡萄牙语写的)
答案 0 :(得分:1)
TalesOfMyroth是Class
名称。所以你应该把它当作一个班级。
static void Main(string[] args)
{
TalesOfMyroth Tales = new TalesOfMyroth();
}
如果您为程序分隔Main class
和Class
,那也很好。所以你会有2个cs文件。 1代表Main Class
,另一代代表TalesOfMyroth class
。然后将这两个文件放在相同的namespace
下。
如果您只想拨打function
,则应将void
添加到function
,并使用与class name
不同的名称
namespace Tales_Of_Myroth
{
public class TalesOfMyroth
{
static void Main(string[] args)
{
Tales();
}
static private Jogador _jogador;
public static void Tales()
{
_jogador = new Jogador();
_jogador.VidaAtual = 50;
_jogador.VidaMaxima = 50;
_jogador.Ouro = 0;
Console.WriteLine("Vida: " + _jogador.VidaAtual);
}
}
}
答案 1 :(得分:0)
此处TalesOfMyroth
是一个类,因此public void Tales()
将是该类的构造函数。根据{{3}},您不能明确调用它。它将在创建相应类的对象时自动调用。
你可以在这个屏幕上做什么来摆脱错误,通过给出一个返回值使它成为一个方法(构造函数不会返回一个值,然后它将被视为一个方法)。但是你不应该在静态main中访问非静态方法。所以你需要将它作为静态方法(需要将_jogador也改为静态)。所以方法将成为:
public static void TalesOfMyroth()
{
// Code here
}
然后,您可以像main
一样访问该方法,就像您正在做的那样。
删除此错误的另一种方法是:在main中创建类的实例。然后TalesOfMyroth
将自动调用。