大纲

  • 前22讲的简要总结:封装,现实世界的东西抽象出来封装在类里面
  • 什么是“类”:是一种数据结构,是一种数据类型,代表现实世界中的“种类”

《C#语言规范》类是一种数据结构,它可以包含数据成员(常量和字段)、函数成员(方法、属性、事件、索引器、运算符、实例构造函数、静态构造函数和析构函数)以及嵌套类型。类类型支持继承,继承是一种机制,它使派生类可以对基类进行扩展和专用化。

  • 构造器与析构器(实例与静态)
  • 后面:继承和多态

类的概念

  • 类是一种抽象数据结构(data structure),枢纽和支点
  • 类是一种数据类型,类是一种引用类型,每一个类都是一个自定义类型。
  • 代表现实世界中的“种类” ```csharp using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;

namespace ClassDefinition { public delegate void Report(); class Program { static void Main(string[] args) { Student student = new Student(1, “Timothy”); Report report = new Report(student.Report); report.Invoke(); } }

  1. class Student
  2. {
  3. //实例构造器
  4. public Student(int id,string name)
  5. {
  6. this.ID = id;
  7. this.Name = name;
  8. }
  9. public int ID { get; set; }
  10. public string Name { get; set; }
  11. public void Report()
  12. {
  13. Console.WriteLine($"I'm #{this.ID} student ,my name is {this.Name}");//C#6$新语法解析
  14. }
  15. }

}

  1. <a name="nXg7N"></a>
  2. # 实例构造器、析构器
  3. 析构器用于手动释放资源。<br />格式:~类名(){}<br />当程序执行到new完student实例后则消失了,对象就没有引用了,这时候执行析构器,释放资源。
  4. ```csharp
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace 析构器
  11. {
  12. class Program
  13. {
  14. static void Main(string[] args)
  15. {
  16. Student student = new Student(1, "Albert");
  17. }
  18. }
  19. class Student
  20. {
  21. public string m_Name { get; set; }
  22. public int m_Number { get; set; }
  23. public Student(int number,string name)
  24. {
  25. this.m_Name = name;
  26. this.m_Number = number;
  27. }
  28. //析构器
  29. ~Student()
  30. {
  31. Console.WriteLine("Byebye!Release the system resource...");
  32. }
  33. }
  34. }

反射基础

类是一种类型。

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace 析构器
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. Student student = new Student(1, "Albert");
  13. //反射 展现类是一种类型(模板),引用类型,自定义类型
  14. Type t = typeof(Student);
  15. //Activator:包含特定的方法,用以在本地或从远程创建对象类型,
  16. //或获取对现有远程对象的引用。 此类不能被继承。
  17. object o = Activator.CreateInstance(t,1,"Jack");
  18. Student student1 = o as Student;
  19. Console.WriteLine(student1.m_Name);
  20. }
  21. }
  22. class Student
  23. {
  24. public string m_Name { get; set; }
  25. public int m_Number { get; set; }
  26. public Student(int number,string name)
  27. {
  28. this.m_Name = name;
  29. this.m_Number = number;
  30. }
  31. //析构器
  32. ~Student()
  33. {
  34. Console.WriteLine("Byebye!Release the system resource...");
  35. }
  36. }
  37. }

Dynamic编程

Dynamic编程

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace 析构器
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. Student student = new Student(1, "Albert");
  13. //反射 展现类是一种类型(模板),引用类型,自定义类型
  14. Type t = typeof(Student);
  15. //Activator:包含特定的方法,用以在本地或从远程创建对象类型,
  16. //或获取对现有远程对象的引用。 此类不能被继承。
  17. dynamic o = Activator.CreateInstance(t,1,"Jack");
  18. Console.WriteLine(o.m_Name);
  19. }
  20. }
  21. class Student
  22. {
  23. public string m_Name { get; set; }
  24. public int m_Number { get; set; }
  25. public Student(int number,string name)
  26. {
  27. this.m_Name = name;
  28. this.m_Number = number;
  29. }
  30. //析构器
  31. ~Student()
  32. {
  33. Console.WriteLine("Byebye!Release the system resource...");
  34. }
  35. }
  36. }

静态构造器

static Student(){}