为什么会显示CS0120错误?我弄不明白

huangapple go评论88阅读模式
英文:

Why is the CS0120 error showing up? I cant figure it out

问题

I will only translate the code for you without addressing your request. Here's the translated code:

namespace LEARNING
{
    public class Human
    {
        public string nameOne;
        public string nameTwo;
        public int ageOne;
        public bool hasagetwo;
        public int ageTwo;
        public bool isalive;

        public void Info()
        {
            Console.WriteLine("姓名 =" + nameOne + "," + nameTwo);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {

            /* 类相关的内容 */

            Human humanOne = new Human();

            humanOne.nameOne = "Adrian";
            humanOne.hasagetwo = true;
            Console.WriteLine(humanOne.nameOne);

            humanOne.Info();

            /* 基本的内容在下面 */
            const string stringOne = "String";
            const string notes = "StringOne的值是:";

            Console.WriteLine(notes);

            Console.WriteLine(stringOne == "StringOne");

            int a = 10;
            int b = 3;
            int c = 11;

            Console.WriteLine(++a);
            Console.WriteLine(a - b);
            Console.WriteLine(a == c);
            Console.WriteLine(b != a);

            Console.WriteLine(a = c);

            Console.WriteLine(a != c && a == 10);
            Console.WriteLine(b != 6 || a == c);

            Console.WriteLine(!(a == b));

            /* 在下面是类相关的内容 */
            Human Adrian = new Human();
        }

    }

}

The translation has been provided.

英文:
namespace LEARNING
{
    public class Human
    {
        public string nameOne;
        public string nameTwo;
        public int ageOne;
        public bool hasagetwo;
        public int ageTwo;
        public bool isalive;

        public void Info()
        {
            Console.WriteLine("Name =" + nameOne + "," + nameTwo);
        }
    }
    class Program
        {
            static void Main(string[] args)
            {

            /* Class Stuff */

            Human humanOne = new Human();

            Human.nameOne = "Adrian";
            Human.hasagetwo = true;
            Console.WriteLine(humanOne.nameOne);

            Human.Info();

            /* Basic Things Down There */
            const string stringOne = "String";
                const string notes = "The Fact that String One has the Value of 'String One' is :";

                Console.WriteLine(notes);

                Console.WriteLine(stringOne == "StringOne");

                int a = 10;
                int b = 3;
                int c = 11;

                Console.WriteLine(++a);
                Console.WriteLine(a - b);
                Console.WriteLine(a == c);
                Console.WriteLine(b != a);

                Console.WriteLine(a = c);

                Console.WriteLine(a != c && a == 10);
                Console.WriteLine(b != 6 || a == c);

                Console.WriteLine(!(a == b));

                /* Classes Down There */
                Human Adrian = new Human();
            }
     
        }
    
}

> CS0120 C# An object reference is required for the non-static field, method, or property

答案1

得分: 3

以下是翻译好的代码部分:

你试图设置对象属性值但你却错误地通过类来做

Human humanOne = new Human();

humanOne.nameOne = "Adrian";
humanOne.hasagetwo = true;
// ...
humanOne.Info();

考虑一下如果你创建了两个人类对象会怎样如果使用`Human.`来访问你怎么知道你正在设置哪一个唯一的例外是静态字段和方法但要访问实际的实例你需要引用这些实例本身
英文:

You're trying to set object property values, but you are doing it through the class by mistake

        Human humanOne = new Human();

        Human.nameOne = "Adrian";
        Human.hasagetwo = true;
        // ....
        Human.Info();

should be

        Human humanOne = new Human();

        humanOne.nameOne = "Adrian";
        humanOne.hasagetwo = true;
        // ...
        humanOne.Info();

Think about it this way, what if you made 2 human objects? If Human. was the way to access, how would you know which of the two you were setting? The exception to this is static fields and methods, but to access the actual instances you refer to those instances themselves.

答案2

得分: 0

更改

Human.nameOne = "Adrian";
Human.hasagetwo = true;
Console.WriteLine(humanOne.nameOne);

Human.Info();

humanOne.nameOne = "Adrian";
humanOne.hasagetwo = true;
Console.WriteLine(humanOne.nameOne);

humanOne.Info();

您正在尝试将实例变量分配给类型(Human),您必须将它们分配给类型的实例(humanOne)而不是类型本身。

英文:

Change

    Human.nameOne = "Adrian";
    Human.hasagetwo = true;
    Console.WriteLine(humanOne.nameOne);

    Human.Info();

to

    humanOne.nameOne = "Adrian";
    humanOne.hasagetwo = true;
    Console.WriteLine(humanOne.nameOne);

    humanOne.Info();

You are trying to assign instance variables to a type (Human), you have to assign them to the instance of the type (humanOne) instead.

huangapple
  • 本文由 发表于 2020年1月7日 01:08:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/59616212.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定