【问题标题】:creating an instance variable by using class name doesn't work out of Main method [duplicate]使用类名创建实例变量不适用于 Main 方法 [重复]
【发布时间】:2020-10-04 11:57:30
【问题描述】:

当我尝试创建一个实例变量以从 Main 方法创建一个类(对象)的实例时,它不起作用。我不明白为什么?我真的在网上搜索了很多次。

例如

This works

This doesn't work

正如我之前了解到的,关键字“int”、“string”等属于一类。我可以使用 Main() 方法之外的那些关键字(即类)创建一个实例变量。

但是为什么我不能通过使用 Main() 方法之外的类名来创建一个 istance 变量。

我的第二个问题是我真的很想知道为什么我需要一个使用类名的实例变量。

我的意思是当我使用“string”、“int”等关键字创建一个 istance 变量时。我可以想象那个变量的样子。但是当我使用类名创建实例变量时,我无法想象任何事情。

感谢您的关注

【问题讨论】:

  • 那是因为你已经将你的方法声明为Static method
  • 请注意,代码应该作为文本包含在您的问题中,而不是图像。有很多原因,主要是我们无法调试图像。其次,“不起作用”不是对问题的描述。我什至会说“不起作用”是本网站的默认条件 - 如果您的代码运行良好,您为什么要在这里发布?请具体说明:如果您收到错误消息,请向我们提供错误消息。如果您的代码行为不端,请告诉我们其行为不端的原因以及应采取的措施。
  • @John 对不起。我会记住你的建议。当我提出一个新问题时,我会更加小心。谢谢你启发我。
  • @Progman 我会分析并重新回答。感谢您的关注
  • 我将代码写到 C# 中,这是所有者要求的,但在 C# 中不起作用。所以我的问题仍然是继续@Progman

标签: c#


【解决方案1】:

对于第二个问题,在 C# 中,类的实例始终是 reference variable。 (我建议您在互联网上阅读更多相关信息)。因此,在示例中:

class Student
{
   Student st1;
}

str1 可以引用 RAM 中的一个区域。你可以把它想象成一个指向矩形的箭头。箭头是引用矩形的变量,矩形是 RAM 上的位置。通过在类本身内部创建一个类的实例,我们实际上创建了一个指向新矩形的箭头,该矩形也有一个可以引用矩形的箭头,依此类推。

例如,创建节点列表很有用。每个节点引用另一个节点,如下所示:

class Node
{
   public int number;
   public Node next; // Here we create a new arrow to a new rectangle, which has an arrow too
}

Main 方法中:

Node first = new Node();
first.number = 1;

Node second = new Node();
second.number = 2;

first.next = second; // Here we "match" the arrow to a rectangle. The arrow is "first.next", and the rectangle is "second".

Node third = new Node();
third.number = 3;

second.next = third;

// We have a list: 1 -> 2 -> 3 -> null
// The end of the list is null, becuase third.next references to null (it references to no "rectangle")

我们可以用不同的方式来做:

Node first = new Node();
first.number = 1;

first.next = new Node();
first.next.number = 2;

first.next.next = new Node();
first.next.next.number = 3;

// In this way, we have only the "head" of the list ("first"), but we can get any node that in the list by typing "first.next. ....... .next"

// We can also iterate over the list:
Node p = first;
while (p != null) // while we are not in the end of the list
{
   // Do something, like "Console.WriteLine(p.number);" or "p.number++;"
   p = p.next; // We go to the next node
}

这很有用,因为这个列表没有长度限制,我们可以轻松添加节点和删除节点。

例如:

public class Program
{
     public static void Main(string[] args)
     {
          Node first = new Node();
          int counter = 0;
         
          first.number = counter;
          counter++;
         
          // Inialize a list of 10 nodes:
          Node p = first; // We don't want to lost the "head" of our list, "first"
          while (counter < 10)
          {                
              p.next = new Node(); // We create a new node
              p.next.number = counter; // And we initialize it
              
              counter++;
              p = p.next; // We go to the next node
          }
          
          // Now we print all the number in our list:
          p = first; // We don't want to lost the "head" of our list, "first"
          while (p != null)
          {
              Console.WriteLine(p.number);
              p = p.next; // We go to the next node
          }
     }
}

输出:

0
1
2
3
4
5
6
7
8
9

希望对您有所帮助!

【讨论】:

  • 感谢您对@Avi 的关注。但这不是我问的。我问的是:class Student { Student st1;这就是我要问的。我们在哪里使用这个“st1”变量。实际上什么是“st1”变量?
  • @AlpayPalabıyık,很抱歉不理解你。我编辑了我的答案。希望对你有用!
  • 真的非常感谢。它有很大帮助。这就是我要问的。非常感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-03-27
  • 1970-01-01
  • 2017-07-03
  • 2022-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多