如何计算使用静态类变量创建的对象数?

时间:2015-04-19 17:46:42

标签: c#

我创建了一个类的3个对象,我想在控制台上显示我创建了多少个对象(使用静态类变量) - 我该怎么做?

我把public static int count = 0;放在我创建的类中,但根据我创建的类的对象数量,我无法增加它(count++;)。我在main方法中创建了3个对象,并为变量赋予了它们。

这是我在程序中创建的类:

public class Student
        {

           public static int count = 0;
       //     count++;


            private string firstName;

            public string FirstName

            {           
                get { return firstName; }
                set { firstName = value; }
            }


            private string lastName;

            public string LastName
            {
                get { return lastName; }
                set { lastName = value; }
            }
            private string birthDate;

            public string BirthDate
            {
                get { return birthDate; }
                set { birthDate = value; }
            }
        }

在main方法中,我创建了3个类Student的对象:

static void Main(string[] args)
        {

         //  Create 3 students    
            Student student1 = new Student       
            {
                FirstName = "John",
                LastName = "Wayne",
                BirthDate = "26/05/1907"

            };

            Student student2 = new Student
            {
                FirstName = "Craig",
                LastName = "Playstead",
                BirthDate ="01/01/1967"
            };

            Student student3 = new Student
            {
                FirstName = "Paula",
                LastName = "Smith",
                BirthDate = "01/12/1977"
            };


            // Console.WriteLine("The course contains {1} students(s) " studentCounter );

根据我创建对象的方式,我无法得到++的计数器。

4 个答案:

答案 0 :(得分:12)

增加构造函数中的计数:

public class Student
{
    public static int count = 0;
    public Student()
    {
        // Thread safe since this is a static property
        Interlocked.Increment(ref count);
    }

    // use properties!
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string BirthDate { get; set; }
}

答案 1 :(得分:1)

你只需要一个构造函数,你就可以增加计数。

public Student()
{
    count++;
}

答案 2 :(得分:1)

您可以在构造函数中增加计数器

public Student()
{
     count++;
}

答案 3 :(得分:-1)

打印计数变量 我们应该写一些像下面这样的代码

public static int GetCount()
{
   return count;
}

和主类看起来像:

static void Main(string[] args)
        {

         //  Create 3 students    
            Student student1 = new Student       
            {
                FirstName = "John",
                LastName = "Wayne",
                BirthDate = "26/05/1907"

            };

            Student student2 = new Student
            {
                FirstName = "Craig",
                LastName = "Playstead",
                BirthDate ="01/01/1967"
            };

            Student student3 = new Student
            {
                FirstName = "Paula",
                LastName = "Smith",
                BirthDate = "01/12/1977"
            };

       //To print the count
       Console.WriteLine(" Number of Objects is : "+Student.GetCount());

}

如果我们有参数化的构造函数,那么我们还必须在该构造函数中编写 count++。