using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ClassExample2
{
class Program
{
static void Main(string[] args)
{
Student student = new Student(1,"Alice");
Console.WriteLine(student.ID);
Console.WriteLine(student.Name);
Console.WriteLine(Student.amount);
}
}
class Student
{
public int ID { get; set; } //学生UD
public string Name { get; set; } //学生姓名
public Student(int ID,string Name)
{
this.ID = ID;
this.Name = Name;
}
public static int amount; //学生总数,总数属于全体学生类所有,所以这里用静态表示。
static Student()
{
amount = 20; //静态构造器
}
~ Student() //实例析构器
{
Console.WriteLine("Byebye");
}
}
}
这里的带两个参数的为实例构造器,为创建实例的时候初始化实例对象,第二个为静态构造器,专门初始化静态成员,最后为实例析构器,当实例对象被回收时自动调用。