C# 类错误-方法需要对象引用

C# 类错误-方法需要对象引用,c#,C#,我目前正在学习C#,并试图为下周的课程做准备,课程将介绍课程和方法。为此,我尝试构建一个名为MaxBox的类,它是一个通用实用程序类,可以存储一些通用函数,如“显示字符串”或“再次播放”。我构建了主文件(程序)和类文件(MaxBox),第23行、第28行和第59行返回相同的一般错误“非静态字段、方法或属性“Program.MaxBox.DisplayStr(string)”需要对象引用”#57返回类似错误“非静态字段、方法或属性”program.MaxBox.PlayReach()需要对象引用”

我目前正在学习C#,并试图为下周的课程做准备,课程将介绍课程和方法。为此,我尝试构建一个名为MaxBox的类,它是一个通用实用程序类,可以存储一些通用函数,如“显示字符串”或“再次播放”。我构建了主文件(程序)和类文件(MaxBox),第23行、第28行和第59行返回相同的一般错误“非静态字段、方法或属性“Program.MaxBox.DisplayStr(string)”需要对象引用”#57返回类似错误“非静态字段、方法或属性”program.MaxBox.PlayReach()需要对象引用”

我真的是个十足的新手,我正在与对象搏斗,我已经做了一些研究来让自己走到这一步,但我对语言的理解还不够,以至于我还不能理解我读过的资源中说了些什么,我想我可以解决这个错误。非常感谢您的帮助和指导。我仍然在我的第一个星期,真的我什么都不知道

节目:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading; // needed for close
using System.Threading.Tasks;

namespace a020_Breakcase_MeaningOfNames_C
{
class Program
{
    public void Play()
    {
        /*
         * Conditionals - Use switch/Case statement too:
         * Evaluate user data (name)
         * Return meaning of name evaluated
         * OR
         * Return 'Name not found' error message
         * Say goodbye
         */

        MaxBox.DisplayStr("What's in a name? Let's find out!");
        Console.Write("\n\n");

        do
        {
            MaxBox.DisplayStr("Enter Name: ");

            string uName = Console.ReadLine().ToLower();
            switch (uName)
            {
                case "doyle":
                    Console.WriteLine("Doyle means descendant of Dubhghalle");
                    break;
                case "fiona":
                    Console.WriteLine("Fiona is considered to be a Latinised form of the Gaelic word fionn, meaning \"white\", \"fair\".");
                    break;
                case "hunter":
                    Console.WriteLine("Hunter means to search with purpose");
                    break;
                case "liam":
                    Console.WriteLine("This name is a short form of the Irish name Uilliam (William) which is now use independently as a given name. As a Hebrew name, Liam means \"my people; I have a nation\".");
                    break;
                case "max":
                    Console.WriteLine("Short for of Maximilian, Maxwell, and the various name using it as a first syllable.");
                    break;
                case "portia":
                    Console.WriteLine("It is of Latin origin. Feminine form of a Roman clan name. Portia was used by Shakespeare as the name of a clever, determined young heroine in \"The Merchant of Venice\" who disguises herself as a lawyer to save her husband's life.");
                    break;

                default:
                    Console.WriteLine("I'm sorry but I don't know the meaning of the name " + uName + ".");
                    break;
            }

        } while (MaxBox.PlayAgain());

        MaxBox.DisplayStr("C#eers!");
    }


    static void Main(string[] args)
    {
        Program myProgram = new Program();
        myProgram.Play();

        Console.Read();
    }
}
}
我的班级:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace a020_Breakcase_MeaningOfNames_C
{
class MaxBox
{
    /*
     * MaxBox is my general functions class
     * Contains 
     * DisplayStr() - Display the string given
     * PlayAgain() - If True, runs program again
     */

    public String uName;
    public String command;

    public void DisplayStr(String StrTxt)
    { Console.Write(StrTxt); }

    public Boolean PlayAgain()
    {
        Console.Write("\n\nDo you want to play again? (y)es or (n)o: ");
        String command = Console.ReadLine().ToLower().Trim();

        if (command == "y" || command == "yes") return true;
        return false;
    }

}
}

MaxBox的方法不是静态的,您需要它的一个实例

MaxBox maxBox = new MaxBox();
在你的主课开始的时候。然后

maxBox.DisplayStr(....)
此外,为了让您思考,您可以替换:

if (command == "y" || command == "yes") return true;
    return false;


MaxBox的方法不是静态的,您需要它的一个实例

MaxBox maxBox = new MaxBox();
在你的主课开始的时候。然后

maxBox.DisplayStr(....)
此外,为了让您思考,您可以替换:

if (command == "y" || command == "yes") return true;
    return false;


方法
playreach
DisplayStr
是类型
MaxBox
上的实例方法。要调用它们,您需要调用它们的
MaxBox
实例。现在,您正试图通过仅适用于静态方法的类型名来调用它们

MaxBox.DisplayStr("hello world");  // wrong
MaxBox mb = new MaxBox();
mb.DisplayStr("hello world");  // right
可以定义方法,以便从类型名调用它们。但这样做需要将它们标记为
static

class MaxBox {
  public static void DisplayStr2(string str){ ... }
}

MaxBox.DisplayStr2("hello world");  // right

方法
playreach
DisplayStr
是类型
MaxBox
上的实例方法。要调用它们,您需要调用它们的
MaxBox
实例。现在,您正试图通过仅适用于静态方法的类型名来调用它们

MaxBox.DisplayStr("hello world");  // wrong
MaxBox mb = new MaxBox();
mb.DisplayStr("hello world");  // right
可以定义方法,以便从类型名调用它们。但这样做需要将它们标记为
static

class MaxBox {
  public static void DisplayStr2(string str){ ... }
}

MaxBox.DisplayStr2("hello world");  // right

使用str1.Equals(str2)来测试字符串等式您应该实例化
MaxBox
类,或者将其更改为static使用str1.Equals(str2)来测试字符串等式您应该实例化
MaxBox
类,或者将其更改为static可能看起来像是一个小细节,但无论
MaxBox
是否是
static
,都没有关系,只是他试图访问的成员不是。什么是静态?如果不是静态的,他为什么需要一个实例?如果你想知道-谢谢-我对课程太陌生了,以至于我完全忘了在我的课程中“介绍”(证实?)我的课程。非常感谢。谢谢大家给我的建议。上周,我为我的上一个项目构建了一个超级简单的类,我正在再次尝试,所以当我们开始上课的时候,我会准备好,因为我听说他们很困惑——而且他们没有上周那么困惑——再次超级感谢!另外,谢谢你给我建议如何回复我的命令,这是非常好的老板,给了我很多思考。非常感谢。可能看起来是一个小细节,但无论
MaxBox
是否是
static
,都无关紧要,只是他试图访问的成员不是。什么是static?如果不是静态的,他为什么需要一个实例?如果你想知道-谢谢-我对课程太陌生了,以至于我完全忘了在我的课程中“介绍”(证实?)我的课程。非常感谢。谢谢大家给我的建议。上周,我为我的上一个项目构建了一个超级简单的类,我正在再次尝试,所以当我们开始上课的时候,我会准备好,因为我听说他们很困惑——而且他们没有上周那么困惑——再次超级感谢!另外,谢谢你给我建议如何回复我的命令,这是非常好的老板,给了我很多思考。非常感谢。感谢您的帮助-非常有用的评论,非常感谢。感谢您的帮助-非常有用的评论,非常感谢。