zoukankan      html  css  js  c++  java
  • C#中反射的基础基础基础

        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; }
        }
    
    
  • 相关阅读:
    动态规划小练
    组合计数小练
    【WC2019】 通道
    【PKUSC2018】主斗地
    【NOI2009】诗人小G
    【THUWC 2017】随机二分图
    【NOI2017】游戏与2-sat方案输出
    Codeforces 1109D sasha and interesting fact from graph theory
    Codeforces 1152E neko and flashback
    ZJOI2019游记
  • 原文地址:https://www.cnblogs.com/maomaodesu/p/11595667.html
Copyright © 2011-2022 走看看