【问题标题】:Difference between class and instance variables in C#C#中类变量和实例变量的区别
【发布时间】:2020-05-09 14:37:22
【问题描述】:

我正在完成基础编程课程,但无法理解类变量和实例变量(静态和非静态)之间的区别。我需要在代码中确定两者的区别

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

我相信 rollNumber、studentName、19236 和“Thomas”都是实例变量,firststudent 是类变量。

由于课程材料不是很有帮助,因此我们将不胜感激。

【问题讨论】:

标签: c# instance-variables static-variables class-variables


【解决方案1】:

类变量: 只有一个副本由一个类的所有不同对象共享,

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

实例变量: 每个对象都有它自己的实例变量的个人副本。因此,跨不同对象的实例变量可以有不同的值,而跨不同对象的类变量只能有一个值。

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

类和实例变量都是成员变量

【讨论】:

    【解决方案2】:

    实例(或非静态)变量

    同一个实例变量的值可以与对该类的引用数一样多,所以如果你有一个这样的类并且你实例化它多次,那么你可以改变每个实例变量(名称)的值每个对象实例(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);
       }
    }
    

    输出:

    My Name
    Your Name
    

    类(或静态)变量

    另一方面,类(或静态)变量只能通过限定名称空间访问,因此您始终访问的是相同的值(实际上您无法通过类。如果你尝试它会显示错误)。你需要它的完整“地址”:

    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);
        }
    
    }
    

    输出:

    PUFF!!
    

    【讨论】:

      【解决方案3】:

      类中定义为字段的所有内容都是关联的实例。在下面的类中,rollnumber、studentName 都是实例关联的。

      Class StudentDetails
      {
        int rollNumber;
        string studentName;
      }
      
      StudentDetails student = new StudentDetails();
      
      

      如果你在类中定义了一个静态字段,那么它就与类相关联。

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

      这里,静态字段 StudentClassNumber 与类相关联,而不是与实例相关联。

      如果你想实例化你提到的StudentDetails,那么你需要在类定义中定义一个非默认构造函数。

      Class StudentDetails
      {
        int rollNumber;
        string studentName;
      
        public StudentDetails(int rollnumber, string studentName)
        {
            this.rollNumber = rollnumber;
            this.studentName = studentName;
      
        }
      
      }
      
      StudentDetails firststudent= StudentDetails (19236, "Thomas");
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-15
        • 2012-12-17
        • 1970-01-01
        • 2017-02-15
        • 2011-05-07
        • 1970-01-01
        相关资源
        最近更新 更多