class Program
{
static void Main(string[] args)
{
Type t = typeof(Student);//typeof(类) 取类的类型 并且存储在Type类型的t变量(其实是把类的类型的引用存在t中)
//t stu1 = new t();这样是不对的
object o = Activator.CreateInstance(t, 1, "One");//通过取到的t创建一个实例(包含非默认构造函数),CreateInstance创建的实例都是Object类型
//o.Name;这种是不存在的,类型因为已经丢失了
//o stu2 = new o();这样是不对的
Student stu3 = (Student)o;
Student stu4 = o as Student;
stu3.ID = 3;
stu3.NAME = "Three";
Console.WriteLine(stu3.ID);
Console.WriteLine(stu3.NAME);
Console.WriteLine(Student.Amount);
Student stu5 = new Student(5, "Five");
Console.WriteLine(Student.Amount);
}
}
class Student
{
public static int Amount { get; set; }
// 静态构造器,只能构造静态成员,因为构建时还没有对象可以操作
static Student() {
Amount = 100;
}
// 动态构造器,对对象进行操作
public Student(int id, string name)
{
this.ID = id;
this.NAME = name;
Student.Amount++;//静态变量不需要定义对象,用类名调用(这里Student.可以省略,因为是在Student类中调用)
}
public int ID { get; set; }
public string NAME { get; set; }
}