非静态字段,方法或属性需要对象引用 [英] An object reference is required for the nonstatic field, method, or property

查看:143
本文介绍了非静态字段,方法或属性需要对象引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道人们以前曾经问过这个问题,但是这种情况太具体了,我对基本原理感到困惑。

I know people have asked about this question before but the scenario's were too specific and I am confused about the fundamentals.

我有两个C#程序的基本版本,一种有效,而一种无效。如果有人可以解释为什么我会收到错误第二个程序中的非静态字段,方法或属性,则需要对象引用。

I have two basic versions of a C# program, one that works, and one that doesn't. I would love it if someone could please explain why I get the error An object reference is required for the nonstatic field, method, or property in the second program.

作品:

namespace Experiments
{
    class Test
    {
        public string myTest = "Gobbledigook";

        public void Print()
        {
            Console.Write(myTest);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Test newTest = new Test();
            newTest.Print();
            while (true)
                ;
        }
    }
}

不起作用:

namespace Experiments
{
    class Test
    {
        public string myTest = "Gobbledigook";

        public void Print()
        {
            Console.Write(myTest);
        }
    }

    class Program
    {
        public Test newTest = new Test();

        static void Main(string[] args)
        {
            newTest.Print();
            while (true)
                ;
        }
    }
}

当我尝试 Print()来自第二个程序中 Test()类的文本,它使我错误非静态字段,方法或属性需要对象引用,我不明白为什么。我可以看到它与声明Test()类的实例有关,但是我不记得在C ++中发生过类似的事情,所以这使我感到困惑。

When I try to Print() the text from the Test() class in the second program, it gives me that error An object reference is required for the nonstatic field, method, or property, and I don't understand why. I can see it has to do with where I declare an instance of the Test() class, but I don't remember anything like this happening in C++, so it mystifies me.

这是怎么回事?

推荐答案

这不是因为该类的定义,全都与关键字 static

It is not because of the definition of the class, it is all about the usage of keyword static.

newTest 类的对象c $ c> Test 是类 Program 的公共成员,而 main 是程序类内部的静态函数。错误消息中明确提到了非静态方法需要对象引用。因此,您需要将 newTest 对象声明为静态对象,以便以诸如main之类的静态方法访问它们。

The newTest object of the class Test is a public member of the class Program and the main is a static function inside the program class. and it is clearly mentioned in the error message An object reference is required for the non-static method. So what you need is Declare the newTest object as static in order to access them in static methods like main.

像这样

 public static Test newTest = new Test();

附加说明

考虑到您要在类 Test中将方法 Print 定义为 static 如下所示:

Consider that you ware defining the method Print as static inside the class Test as like the following:

 public static void Print()
 {
    Console.Write(myTest);
 }

然后您不能像当前使用的那样调用该方法( newTest.Print(); )。取而代之的是,您必须使用 Test.Print(); ,因为不能通过实例引用静态成员。而是通过类型名称引用它。例如,考虑以下类

Then you cant call the method like what you are currently using(newTest.Print();). Instead of that you have to use Test.Print();, Since A static member cannot be referenced through an instance. Instead, it is referenced through the type name. For example, consider the following class

这篇关于非静态字段,方法或属性需要对象引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆