简体   繁体   English

C#中类变量和实例变量的区别

[英]Difference between class and instance variables in C#

I am completing a basics programming course and I am having trouble understanding the difference between class variable and instance variables (static and non-static).我正在完成基础编程课程,但无法理解类变量和实例变量(静态和非静态)之间的区别。 I need to determine the difference between the two in the code我需要在代码中确定两者之间的区别

Class StudentDetails
{
Int rollNumber;
String studentName;
}
StudentDetails firststudent= StudentDetails (19236, ”Thomas”);

I believe the rollNumber, studentName, 19236 and "Thomas" are all instance variables and the firststudent is a class variable.我相信 rollNumber、studentName、19236 和“Thomas”都是实例变量,而 firststudent 是一个类变量。

Any assistance on this would be appreciated as the course material is not very helpful.由于课程材料不是很有帮助,因此我们将不胜感激。

Class variables: only have one copy that is shared by all the different objects of a class,类变量:只有一个副本被一个类的所有不同对象共享,

class StudentDetails
{
  static Int rollNumber;
  /*...*/
}

Instance variable: Every object has it's own personal copy of an instance variable.实例变量:每个对象都有自己的实例变量的个人副本。 So, instance variables across different objects can have different values whereas class variables across different objects can have only one value.因此,跨不同对象的实例变量可以具有不同的值,而跨不同对象的类变量只能具有一个值。

class StudentDetails
{
  Int rollNumber;
  /*...*/
}

Class and Instance variables are both Member variables类和实例变量都是成员变量

Instance (or NON-Static) Variables实例(或非静态)变量

The same instance variable can have as many values as the number of references to that class, so if you have a class like this and you instatiate it several times, then you can change the value of each instance variable (name) per each object instance (foo and bar):同一个实例变量可以有与该类的引用数量一样多的值,所以如果你有一个这样的类并且你将它实例化了几次,那么你可以改变每个对象实例的每个实例变量(名称)的值(foo 和 bar):

public class Fancyclass
{
   //this is an instance variable
   string name;

   public static void Main(string[] args)
   {
         Fancyclass foo = new Fancyclass();
         Fancyclass bar = new Fancyclass();
         foo.name = "My Name";
         bar.name = "Your Name";

         Console.WriteLine(foo.name);
         Console.WriteLine(bar.name);
   }
}

Output:输出:

My Name
Your Name

Class (or Static) Variables类(或静态)变量

On the other hand, a class (or static) variable can ONLY be accessed through the qualified namespace, so you are always accessing the same value really (you literally can't access it through a reference/instance/object of the class. It will show an error if you try).另一方面,类(或静态)变量只能通过限定的命名空间访问,因此您实际上总是访问相同的值(您实际上无法通过类的引用/实例/对象访问它。它如果您尝试将显示错误)。 You need it's full "address":你需要它的完整“地址”:

public class ClassExample
{
    //class (or static) variable
    static string name;

    public static void Main(string[] args)
    {
         ClassExample foobar = new ClassExample();
         
         //this will show an error
         foobar.name;
         
         //instead you access it this way
         ClassExample.name = "PUFF!!"

         Console.WriteLine(ClassExample.name);
    }

}

Output:输出:

PUFF!!

Everything defined as a field in the class, is a instance associated.在类中定义为字段的所有内容都是关联的实例。 In the below class, both rollnumber, studentName are instance associated.在下面的类中,rollnumber 和 studentName 都是实例关联的。

Class StudentDetails
{
  int rollNumber;
  string studentName;
}

StudentDetails student = new StudentDetails();

If you define a static field in the class, then it is associated with the class.如果在类中定义静态字段,则它与类相关联。

Class StudentDetails
{
  int rollNumber;
  string studentName;
  static int StudentClassNumber = 123
}

Here, static field StudentClassNumber is associated with the class, not with the instance.在这里,静态字段 StudentClassNumber 与类相关联,而不是与实例相关联。

If you want to instantiate StudentDetails as you have mentioned, then you need to define a non-default constructor in the class definition.如果你想像你提到的那样实例化 StudentDetails,那么你需要在类定义中定义一个非默认构造函数。

Class StudentDetails
{
  int rollNumber;
  string studentName;

  public StudentDetails(int rollnumber, string studentName)
  {
      this.rollNumber = rollnumber;
      this.studentName = studentName;

  }

}

StudentDetails firststudent= StudentDetails (19236, "Thomas");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM