1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace 集合 7 { 8 class Student 9 { 10 //學生id 11 private int sid; 12 13 public int Sid 14 { 15 get { return sid; } 16 set { sid = value; } 17 } 18 //學生姓名 19 private string name; 20 21 public string Name 22 { 23 get { return name; } 24 set { name = value; } 25 } 26 //班級 27 private string class1; 28 29 public string Class1 30 { 31 get { return class1; } 32 set { class1 = value; } 33 } 34 //學生成績(html) 35 private double html; 36 37 public double Html 38 { 39 get { return html; } 40 set { html = value; } 41 } 42 //學生成績(c) 43 private double c; 44 45 public double C 46 { 47 get { return c; } 48 set { c = value; } 49 } 50 //學生成績(sql) 51 private double sql; 52 53 public double Sql 54 { 55 get { return sql; } 56 set { sql = value; } 57 } 58 59 60 //構造函數(用來以名字來查詢的) 61 public Student(string name) 62 { 63 this.Name = name; 64 } 65 //構造方法(用來以學號來查詢的和刪除學生) 66 public Student(int sid) 67 { 68 this.Sid = sid; 69 } 70 71 //構造方法(用來添加學生信息的) 72 public Student(int sid, string name, string class1, double c, double html, double sql) 73 { 74 this.Sid = sid; 75 this.Name = name; 76 this.Class1 = class1; 77 this.C = c; 78 this.Html = html; 79 this.Sql = sql; 80 } 81 /// <summary> 82 /// 重寫object的Tostring方法 83 /// </summary> 84 /// <returns></returns> 85 public override string ToString() 86 { 87 //順序決定顯示的順序 88 return this.sid.ToString()+"\t" + name +"\t"+class1+"\t" + c +"\t"+html+"\t" +sql ; 89 } 90 /// <summary> 91 /// 重寫Object的Equals方法 92 /// </summary> 93 /// <param name="obj"></param> 94 /// <returns></returns> 95 public override bool Equals(object obj) 96 { 97 // 判斷是否屬於Stundet數據類型(除開基本類型外其餘的都是引用類型) 98 if (!(obj is Student)) 99 { 100 return false; 101 } 102 //姓名是否相等(this.name這裏不是指當前一個對象,而是指當前這個集合的對象) 103 if (this.name.Equals(((Student)obj).name)) 104 { 105 return true; 106 } 107 //學號(它會遍歷集合) 108 if (this.sid.Equals(((Student)obj).sid)) 109 { 110 return true; 111 } 112 return false; 113 } 114 } 115 }
Student stu=new Student();html
Console.WriteLine(stu);//執行的結果爲你重寫toString方法的結果而不是Student這個對象sql