c# 操作符重载
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class Namer 2 { 3 private string FirstName; 4 private string LastName; 5 6 public Namer() 7 { 8 this.Init(null, null); 9 } 10 11 public Namer(string fn, string ln) 12 { 13 this.Init(fn, ln); 14 } 15 16 private void Init(string fn, string ln) 17 { 18 this.FirstName = fn; 19 this.LastName = ln; 20 } 21 22 /// <summary> 23 /// 重载运算符true 24 /// </summary> 25 /// <param name="n"></param> 26 /// <returns>如果姓名中姓或名有一个为空,就返回假</returns> 27 public static bool operator true(Namer n) 28 { 29 if (n.FirstName == null || n.LastName == null) 30 return false; 31 else 32 return true; 33 } 34 35 /// <summary> 36 /// 重载运算符false 37 /// </summary> 38 /// <param name="n"></param> 39 /// <returns></returns> 40 public static bool operator false(Namer n) 41 { 42 if (n.FirstName == null || n.LastName == null) 43 return false; 44 else 45 return true; 46 } 47 48 /// <summary> 49 /// 重载运算符== 50 /// </summary> 51 /// <param name="n1"></param> 52 /// <param name="n2"></param> 53 /// <returns></returns> 54 public static bool operator ==(Namer n1, Namer n2) 55 { 56 if (n1.FirstName == n2.FirstName && n1.LastName == n2.LastName) 57 return true; 58 else 59 return false; 60 } 61 62 /// <summary> 63 /// 重载运算符!= 64 /// </summary> 65 /// <param name="n1"></param> 66 /// <param name="n2"></param> 67 /// <returns></returns> 68 public static bool operator !=(Namer n1, Namer n2) 69 { 70 if (n1.FirstName != n2.FirstName || n1.LastName != n2.LastName) 71 return true; 72 else 73 return false; 74 } 75 76 /// <summary> 77 /// 重载运算符> 78 /// </summary> 79 /// <param name="n1"></param> 80 /// <param name="n2"></param> 81 /// <returns></returns> 82 public static bool operator >(Namer n1, Namer n2) 83 { 84 return (n1.FirstName.CompareTo(n2.FirstName) > 0 && n1.LastName.CompareTo(n2.LastName) > 0); 85 } 86 87 /// <summary> 88 /// 重载运算符< 89 /// </summary> 90 /// <param name="n1"></param> 91 /// <param name="n2"></param> 92 /// <returns></returns> 93 public static bool operator <(Namer n1, Namer n2) 94 { 95 return (n1.FirstName.CompareTo(n2.FirstName) < 0 && n1.LastName.CompareTo(n2.LastName) < 0); 96 } 97 98 /// <summary> 99 /// 重写方法,必须有,后面的111是乱加的,你也可以写其它的 100 /// </summary> 101 /// <returns></returns> 102 public override int GetHashCode() 103 { 104 return base.GetHashCode() * 111; 105 } 106 107 /// <summary> 108 /// 重写方法,必须有 109 /// </summary> 110 /// <param name="obj"></param> 111 /// <returns></returns> 112 public override bool Equals(object obj) 113 { 114 return base.Equals(obj); 115 } 116 117 /// <summary> 118 /// 重写方法,必须有 119 /// </summary> 120 /// <returns></returns> 121 public override string ToString() 122 { 123 return "姓:" + this.FirstName + " 名:" + this.LastName; 124 } 125 126 127 public static void Main() 128 { 129 Namer n1 = new Namer("li", "zanhong"); 130 Namer n2 = new Namer("ahang", "aan"); 131 //Namer n2=new Namer("li","zanhong"); 132 133 if (n1 == n2) 134 Console.WriteLine("一样的名字"); 135 else 136 Console.WriteLine("不一样的名字"); 137 ///////////////////////////////////// 138 if (n1 != n2) 139 Console.WriteLine("不一样的名字成立"); 140 else 141 Console.WriteLine("同样的名字成立"); 142 ///////////////////////////////////// 143 if (n1 > n2) 144 Console.WriteLine(n1.ToString() + ">" + n2.ToString()); 145 else if (n1 < n2) 146 Console.WriteLine(n1.ToString() + "<" + n2.ToString()); 147 else 148 Console.WriteLine("没有正确比较"); 149 150 Console.ReadLine(); 151 } 152 }
工欲善其事,必先利其器。