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; }
        }
    
    
  • 相关阅读:
    CSU 1333 Funny Car Racing
    FZU 2195 检查站点
    FZU 2193 So Hard
    ZOJ 1655 FZU 1125 Transport Goods
    zoj 2750 Idiomatic Phrases Game
    hdu 1874 畅通工程续
    hdu 2489 Minimal Ratio Tree
    hdu 3398 String
    洛谷 P2158 [SDOI2008]仪仗队 解题报告
    POJ 1958 Strange Towers of Hanoi 解题报告
  • 原文地址:https://www.cnblogs.com/maomaodesu/p/11595667.html
Copyright © 2011-2022 走看看