C# 非发票会员';TalesOfMyroth()和#x27;can';不要像一种方法那样使用

C# 非发票会员';TalesOfMyroth()和#x27;can';不要像一种方法那样使用,c#,C#,我想为我刚开始的大学制作一个简单的文字游戏…有人能帮我吗?(代码中有些东西是用葡萄牙语写的)TalesOfMyroth是一个类名称。所以你应该把它当作一个类来使用 namespace Tales_Of_Myroth { public class TalesOfMyroth { static void Main(string[] args) { TalesOfMyroth(); //here is my error

我想为我刚开始的大学制作一个简单的文字游戏…有人能帮我吗?(代码中有些东西是用葡萄牙语写的)

TalesOfMyroth是一个
名称。所以你应该把它当作一个类来使用

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);

        }
    }
}
如果您将程序的
主类
分开,这也很好。所以你会有2个cs文件。1是您的
主类
,另一个是您的
TalesOfMyroth类
。然后将这两个文件放在相同的
命名空间下

如果只想调用
函数
,则应将
void
添加到
函数
,并使用与
类名不同的名称

static void Main(string[] args)
    {
        TalesOfMyroth Tales = new TalesOfMyroth();
    }

TalesOfMyroth是一个
名称。所以你应该把它当作一个类来使用

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);

        }
    }
}
如果您将程序的
主类
分开,这也很好。所以你会有2个cs文件。1是您的
主类
,另一个是您的
TalesOfMyroth类
。然后将这两个文件放在相同的
命名空间下

如果只想调用
函数
,则应将
void
添加到
函数
,并使用与
类名不同的名称

static void Main(string[] args)
    {
        TalesOfMyroth Tales = new TalesOfMyroth();
    }

这里的
TalesOfMyroth
是一个类,因此
public void Tales()
将是该类的构造函数。根据,不允许显式调用它。当创建相应类的对象时,将自动调用它

在这个屏幕中,您可以做的是,通过给出一个返回值(构造函数不会返回值,那么它将被视为一个方法),将它作为一个方法来处理。但是您不应该访问静态main中的非静态方法。因此,您需要将其设置为静态方法(也需要将_jogador更改为静态)。因此,该方法将成为:

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);

        }
    }
}
然后,您可以从
main
访问该方法,就像您正在做的一样


消除此错误的另一种方法是:在main中创建类的实例。然后会自动调用
TalesOfMyroth
TalesOfMyroth
在这里是一个类,因此
public void Tales()
将是该类的构造函数。根据,不允许显式调用它。当创建相应类的对象时,将自动调用它

在这个屏幕中,您可以做的是,通过给出一个返回值(构造函数不会返回值,那么它将被视为一个方法),将它作为一个方法来处理。但是您不应该访问静态main中的非静态方法。因此,您需要将其设置为静态方法(也需要将_jogador更改为静态)。因此,该方法将成为:

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);

        }
    }
}
然后,您可以从
main
访问该方法,就像您正在做的一样


消除此错误的另一种方法是:在main中创建类的实例。然后将自动调用
TalesOfMyroth

仍然无法从
main
访问
Tales
。你知道为什么吗?@unlucky>添加了
static
。thx直到您无法从
main
访问
Tales
。你知道为什么吗?@unlucky>添加了
static
。谢谢