C# 类
类的声明
1 /// <summary> 2 /// 学校教学班 3 /// </summary> 4 public class SchoolClass 5 { 6 //public Guid ID = Guid.NewGuid(); // 属性的直接处理方式 7 public Guid ID { get; set; } // 教学班标识符,唯一标识符 8 public string Name { get; set; } // 教学班名称 9 public string Description { get; set; } // 教学班简要说明 10 public string SortCode { get; set; } // 业务编码 11 public DateTime CreateTime { get; set; } // 创建日期 12 public GradeEnum Grade { get; set; } // 归属年级 13 public bool IsActive { get; set; } // 是否为在校教学班(否表示已不是) 14 15 public SchoolClass() 16 { 17 this.ID = Guid.NewGuid(); 18 } 19 } 20 21 /// <summary> 22 /// 学生 23 /// </summary> 24 public class Student 25 { 26 public Guid ID { get; set; } // 唯一标识符 27 public string Name { get; set; } // 学生姓名 28 public string Description { get; set; } // 简要说明 29 public string SortCode { get; set; } // 学号 30 public bool Sex { get; set; } // 性别 31 public DateTime BirthDay { get; set; } // 出生日期 32 public string Origin { get; set; } // 籍贯 33 34 public SchoolClass SchoolClass { get; set; } // 归属的教学班 35 36 /// <summary> 37 /// 构造函数,创建实例并为标识符赋值 38 /// </summary> 39 public Student() 40 { 41 this.ID = Guid.NewGuid(); 42 } 43 } 44 45 /// <summary> 46 /// 年级 47 /// </summary> 48 public enum GradeEnum 49 { 50 初一, 初二, 初三, 51 高一, 高二, 高三 52 }
对象:类的实例化
1 /// <summary> 2 /// 演示类和对象构建的一般方法 3 /// </summary> 4 class Program 5 { 6 static void Main(string[] args) 7 { 8 9 #region 传统的创建业务实体对象的写法 10 Student student01 = new Student(); 11 12 student01.Name = "张文华"; 13 student01.Description = "张文华的简要说明"; 14 student01.SortCode = "20160901H001"; 15 student01.Sex = true; 16 student01.BirthDay = DateTime.Parse("1990-08-18"); 17 student01.Origin = "广西桂林"; 18 #endregion 19 20 #region 现代的创建业务实体对象的写法 21 var student02 = new Student() 22 { 23 Name = "张文华", 24 Description = "张文华的简要说明", 25 SortCode = "20160901H001", 26 Sex = true, 27 BirthDay = DateTime.Parse("1990-08-18"), 28 Origin = "广西桂林" 29 }; 30 #endregion 31 } 32 }
类属性的修饰符
private 和 readonly 的简要说明
属性的直接处理方式(注:partial 是定义局部类的关键字,下面会做详细的解释)
字段的属性的概念
(字段)
(属性)
构造函数重构
1 /// <summary> 2 /// 构造函数,创建实例并为标识符赋值 3 /// </summary> 4 public Student() 5 { 6 this.ID = Guid.NewGuid(); 7 #region 在构造函数中可以为私有变量和只读变量赋值 8 //this._PrivateInformation01 = "隐私性质的信息01"; 9 //this._PrivateInformation02 = "隐私性质的信息01"; 10 //this._ReadOnlyInformation = "只读性质的信息"; 11 #endregion 12 } 13 14 /// <summary> 15 /// 重载构造函数,直接为属性赋值 16 /// </summary> 17 /// <param name="name"></param> 18 /// <param name="description"></param> 19 /// <param name="sortcode"></param> 20 /// <param name="sex"></param> 21 /// <param name="birthday"></param> 22 /// <param name="origin"></param> 23 public Student(string name,string description, string sortcode, bool sex, DateTime birthday, string origin) 24 { 25 this.ID = Guid.NewGuid(); 26 this.Name = name; 27 this.Description = description; 28 this.SortCode = sortcode; 29 this.Sex = sex; 30 this.BirthDay = birthday; 31 this.Origin = origin; 32 }
静态类和和静态方法
静态类基本和非静态类相同,但存在一个差异:静态类无法实例化,就是无法使用 new 关键字创建类类型的变量,由于不存在任何实例变量,因此可以使用类名本身访问静态类成员
具体代码如下:
1 /// <summary> 2 /// 对学生数据进行持久化处理的常规方法 3 /// </summary> 4 public static class DataStore 5 { 6 /// <summary> 7 /// 保存 student 对象数据 8 /// </summary> 9 /// <param name="employee"></param> 10 public static void Save(Student student) 11 { 12 // 使用 student 对象的 ID+dat 作为数据存储的文件名,保存对象数据 13 var stream = new FileStream(student.ID + "dat", FileMode.Create); 14 // 通过 StreamWriter 对象将传入的 employee 数据存储到前面创建的文件中 15 using (var writer = new StreamWriter(stream)) 16 { 17 writer.WriteLine(student.ID); 18 writer.WriteLine(student.Name); 19 writer.WriteLine(student.Description); 20 writer.WriteLine(student.SortCode); 21 writer.WriteLine(student.Sex); 22 writer.WriteLine(student.BirthDay); 23 writer.WriteLine(student.Origin); 24 if (student.SchoolClass != null) 25 writer.WriteLine(student.SchoolClass.Name); 26 27 writer.Close(); // 关闭文件流 28 } 29 } 30 31 /// <summary> 32 /// 从存储数据的文件中读取数据并转换为相应的 Student 对象 33 /// </summary> 34 /// <param name="id">对象的 id </param> 35 /// <returns></returns> 36 public static Student Get(Guid id) 37 { 38 var student = new Student(); 39 var stream = new FileStream(id.ToString() + "dat", FileMode.Open); 40 // 通过 StreamReader 读取文件数据,赋值到对象相关属性 41 using(var reader=new StreamReader(stream)) 42 { 43 student.ID = Guid.Parse(reader.ReadLine()); 44 student.Name = reader.ReadLine(); 45 student.Description = reader.ReadLine(); 46 student.SortCode = reader.ReadLine(); 47 student.Sex = Boolean.Parse(reader.ReadLine()); 48 student.BirthDay = DateTime.Parse(reader.ReadLine()); 49 student.Origin = reader.ReadLine(); 50 reader.Close(); 51 return student; 52 53 54 } 55 } 56 57 /// <summary> 58 /// 初始化一批教学班数据,供程序使用 59 /// </summary> 60 /// <returns></returns> 61 public static List<SchoolClass> CreateSchoolClassCollection() 62 { 63 var schoolClassCollection = new List<SchoolClass>(); 64 65 var sc01 = new SchoolClass() 66 { 67 Name = "高一1班", 68 Description = "关于高一1班的说明", 69 SortCode = "20170901H", 70 CreateDate = DateTime.Parse("2017-09-01"), 71 GradeEnum = GradeEnum.高一, 72 IsActive = true 73 }; 74 schoolClassCollection.Add(sc01); 75 76 var sc02 = new SchoolClass() 77 { 78 Name = "高一2班", 79 Description = "关于高一2班的说明", 80 SortCode = "20170902H", 81 CreateDate = DateTime.Parse("2017-09-01"), 82 GradeEnum = GradeEnum.高一, 83 IsActive = true 84 }; 85 schoolClassCollection.Add(sc02); 86 87 var sc03 = new SchoolClass() 88 { 89 Name = "高一3班", 90 Description = "关于高一3班的说明", 91 SortCode = "20170903H", 92 CreateDate = DateTime.Parse("2017-09-01"), 93 GradeEnum = GradeEnum.高一, 94 IsActive = true 95 }; 96 schoolClassCollection.Add(sc03); 97 98 var sc04 = new SchoolClass() 99 { 100 Name = "高二1班", 101 Description = "关于高二1班的说明", 102 SortCode = "20160901H", 103 CreateDate = DateTime.Parse("2016-09-01"), 104 GradeEnum = GradeEnum.高二, 105 IsActive = true 106 }; 107 schoolClassCollection.Add(sc04); 108 109 var sc05 = new SchoolClass() 110 { 111 Name = "高二2班", 112 Description = "关于高一1班的说明", 113 SortCode = "20160902H", 114 CreateDate = DateTime.Parse("2016-09-01"), 115 GradeEnum = GradeEnum.高二, 116 IsActive = true 117 }; 118 schoolClassCollection.Add(sc05); 119 120 var sc06 = new SchoolClass() 121 { 122 Name = "高二3班", 123 Description = "关于高二3班的说明", 124 SortCode = "20170903H", 125 CreateDate = DateTime.Parse("2016-09-01"), 126 GradeEnum = GradeEnum.高二, 127 IsActive = true 128 }; 129 schoolClassCollection.Add(sc06); 130 131 return schoolClassCollection; 132 } 133 }
扩展方法(针对 Student 类的扩展(动态编程))
1 /// <summary> 2 /// 针对 Student 类的扩展(动态编程) 3 /// </summary> 4 public static class StudentExtension 5 { 6 /// <summary> 7 /// 打印学生数据到控制台 8 /// </summary> 9 /// <param name="student">学生对象</param> 10 public static void Print(this Student student) 11 { 12 Console.WriteLine(student.ToString()); 13 } 14 }
特殊的类(内嵌类、局部类)
内嵌类
相关代码
1 /// <summary> 2 /// 学生 3 /// </summary> 4 public class Student 5 { 6 public Guid ID { get; set; } // 唯一标识符 7 public string Name { get; set; } // 学生姓名 8 public string Description { get; set; } // 简要说明 9 public string SortCode { get; set; } // 学号 10 public bool Sex { get; set; } // 性别 11 public DateTime BirthDay { get; set; } // 出生日期 12 public string Origin { get; set; } // 籍贯 13 14 public SchoolClass SchoolClass { get; set; } // 归属的教学班 15 16 //private string _PrivateInformation01; // 私有变量01 17 //private string _PrivateInformation02 { get; } // 私有变量02,并且只能读取 18 //readonly string _ReadOnlyInformation; // 只读性质的信息 19 20 /// <summary> 21 /// 构造函数,创建实例并为标识符赋值 22 /// </summary> 23 public Student() 24 { 25 this.ID = Guid.NewGuid(); 26 #region 在构造函数中可以为私有变量和只读变量赋值 27 //this._PrivateInformation01 = "隐私性质的信息01"; 28 //this._PrivateInformation02 = "隐私性质的信息01"; 29 //this._ReadOnlyInformation = "只读性质的信息"; 30 #endregion 31 } 32 33 /// <summary> 34 /// 重载构造函数,直接为属性赋值 35 /// </summary> 36 /// <param name="name"></param> 37 /// <param name="description"></param> 38 /// <param name="sortcode"></param> 39 /// <param name="sex"></param> 40 /// <param name="birthday"></param> 41 /// <param name="origin"></param> 42 public Student(string name,string description, string sortcode, bool sex, DateTime birthday, string origin) 43 { 44 this.ID = Guid.NewGuid(); 45 this.Name = name; 46 this.Description = description; 47 this.SortCode = sortcode; 48 this.Sex = sex; 49 this.BirthDay = birthday; 50 this.Origin = origin; 51 } 52 53 /// <summary> 54 /// 将对象数据格式化处理,可以用于前端显示 55 /// </summary> 56 /// <returns></returns> 57 public override string ToString() 58 { 59 //this._PrivateInformation01 = "更新后的隐私信息"; 60 //this._PrivateInformation02 = "更新后的隐私信息"; // 不能在方法中更新只读私有变量 61 //this._ReadOnlyInformation = "更新后的只读信息"; // 不能在方法中更新只读变量 62 63 var sexString = "男"; 64 if (!this.Sex) 65 sexString = "女"; 66 var birthday = this.BirthDay.ToLongDateString(); 67 68 var result = string.Format("学生数据: {0} {1} {2} {3} {4} {5}", this.ID, this.Name, this.SortCode, sexString, birthday, this.Origin); 69 return result; 70 } 71 72 /// <summary> 73 /// 内嵌类:联系方式 74 /// </summary> 75 public class Contact 76 { 77 public string Mobile { get; set; } 78 public string EMail { get; set; } 79 public string QQ { get; set; } 80 } 81 }
局部类
相关代码
1 /// <summary> 2 /// 教师 3 /// </summary> 4 public partial class Teacher 5 { 6 public Guid ID { get; set; } // 唯一标识符 7 public string Name { get; set; } // 教师姓名 8 public string Description { get; set; } // 简要说明 9 public string SortCode { get; set; } // 工号 10 public bool Sexe { get; set; } // 性别 11 public DateTime BirthDay { get; set; } // 出生日期 12 public string Origin { get; set; } // 籍贯 13 14 public Teacher() 15 { 16 this.ID = Guid.NewGuid(); 17 } 18 } 19 /// <summary> 20 /// 作为 Teacher 局部类,补充类 Teacher 的属性或方法 21 /// </summary> 22 public partial class Teacher 23 { 24 public DateTime JoiningDate { get; set; } // 入职时间 25 }