C#入门详解 刘铁锰 什么是类

C#---刘铁锰 C#语言入门详解 学习笔记02(P3) (类、名称空间、类库、依赖关系

类是引用类型

析构函数 和构造函数一样放在类里面。

class Program
{
 static void Main(string[] args)
   {
     Studnt stu =new Student(2019,"Ozz"); //主函数 赋值给Stu

   }
}
 class Student
  {
        public string Name { get; set; }
      public int ID { get; set; }
      public void Report()
      {
           Console.WriteLine( $"I'm {ID}student,my name is {Name}." );
       }


      ~Student()
        {
           Console.WriteLine("Bye bye!Release the system resources...");
       }
   }

实例不一定需要new C#是一种托管语言,有 反射。

  static void Main(string[] args)
        {
            Type t = typeof(Student);  //把Student类型存储到 Type类型的t 中
                                       // t stu=new t(); //达咩
            object o = Activator.CreateInstance(t, 201912894, "OZZ"); //object是所有的父类  o.ID 去获取student中的属性是不行的 因为类型已经丢失了
            //类型丢失了,但是可以找回来 
            Console.WriteLine(o.GetType().Name);   //结果是 Student
            Console.WriteLine(o is Student);  //结果是 true;

            //可以把这个类型再找回来  
            //Student stu = (Student)o; //可以强制类型转换,因为已经确定它就是Student类型的
            Student stu = o as Student;
        }

dynamic 标题
static void Main(string[] args)
{
Type t = typeof(Student);
dynamic stu = Activator.CreateInstance(t, 1, "Timothy");
Console.WriteLine(stu.Name);
}

静态成员 是对类有意义的

静态构造器 只能构造(初始化)静态成员 不能构造实例成员
static Student()
{ Amount=100;
}

静态 有哪些特点


静态中不可以有实例成员

posted @ 2022-06-20 16:28  专心Coding的程侠  阅读(118)  评论(0编辑  收藏  举报