英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论