C#重载操作符>> 有什么用?怎么重载?
C#重载操作符>> 有什么用?怎么重载?
using System; using System.Xml; namespace ConsoleApplication8 { /// <summary> /// 姓名类 /// </summary> class Namer { private string FirstName; private string LastName; public Namer() { this.Init(null,null); } public Namer(string fn,string ln) { this.Init(fn,ln); } private void Init(string fn,string ln) { this.FirstName=fn; this.LastName=ln; } /// <summary> /// 重载运算符true /// </summary> /// <param name="n"></param> /// <returns>如果姓名中姓或名有一个为空,就返回假</returns> public static bool operator true(Namer n) { if(n.FirstName==null || n.LastName==null) return false; else return true; } /// <summary> /// 重载运算符false /// </summary> /// <param name="n"></param> /// <returns></returns> public static bool operator false(Namer n) { if(n.FirstName==null || n.LastName==null) return false; else return true; } /// <summary> /// 重载运算符== /// </summary> /// <param name="n1"></param> /// <param name="n2"></param> /// <returns></returns> public static bool operator ==(Namer n1,Namer n2) { if(n1.FirstName==n2.FirstName && n1.LastName==n2.LastName) return true; else return false; } /// <summary> /// 重载运算符!= /// </summary> /// <param name="n1"></param> /// <param name="n2"></param> /// <returns></returns> public static bool operator !=(Namer n1,Namer n2) { if(n1.FirstName!=n2.FirstName || n1.LastName !=n2.LastName) return true; else return false; } /// <summary> /// 重载运算符> /// </summary> /// <param name="n1"></param> /// <param name="n2"></param> /// <returns></returns> public static bool operator >(Namer n1,Namer n2) { return (n1.(n2.FirstName)>0 && n1.(n2.LastName)>0); } /// <summary> /// 重载运算符< /// </summary> /// <param name="n1"></param> /// <param name="n2"></param> /// <returns></returns> public static bool operator <(Namer n1,Namer n2) { return (n1.(n2.FirstName)<0 && n1.(n2.LastName)<0); } /// <summary> /// 重写方法,必须有,后面的111是乱加的,你也可以写其它的 /// </summary> /// <returns></returns> public override int GetHashCode() { return base.GetHashCode()*111; } /// <summary> /// 重写方法,必须有 /// </summary> /// <param name="obj"></param> /// <returns></returns> public override bool Equals(object obj) { return base.Equals (obj); } /// <summary> /// 重写方法,必须有 /// </summary> /// <returns></returns> public override string ToString() { return "姓:"+this.FirstName+" 名:"+this.LastName; } public static void Main() { Namer n1=new Namer("li","zanhong"); Namer n2=new Namer("ahang","aan"); //Namer n2=new Namer("li","zanhong"); if(n1==n2) Console.WriteLine("一样的名字"); else Console.WriteLine("不一样的名字"); ///////////////////////////////////// if(n1!=n2) Console.WriteLine("不一样的名字成立"); else Console.WriteLine("同样的名字成立"); ///////////////////////////////////// if(n1>n2) Console.WriteLine(n1.ToString()+">"+n2.ToString()); else if(n1<n2) Console.WriteLine(n1.ToString()+"<"+n2.ToString()); else Console.WriteLine("没有正确比较"); Console.ReadLine(); } } }
using System; using System.Collections.Generic; using System.Text; namespace ObjectShow { /// <summary> /// 对象输出接口 /// </summary> public interface ICout { /// <summary> /// 对象ID /// </summary> int ObjectID { get; } /// <summary> /// 添加对象到对象字典 /// </summary> void AddObjectToDictionary(string outputString); } }Cout类
using System; using System.Collections.Generic; using System.Text; namespace ObjectShow { /// <summary> /// 输出对象 /// </summary> public class Cout { /// <summary> /// 全局对象:对象字典 /// </summary> private static Dictionary<int, string> Objects = new Dictionary<int, string>(); /// <summary> /// 最近的对象标识 /// </summary> private static int lastObjectID = 0; /// <summary> /// 添加对象到对象字典 /// </summary> public static int AddObject( string msg) { int tempObjectID = lastObjectID; Objects.Add(lastObjectID++, msg); return tempObjectID; } /// <summary> /// 重载移位运算符 /// </summary> public static Cout operator >>(Cout cout, int objectID) { if (cout != null) { if (Objects.ContainsKey(objectID)) { cout.Print(Objects[objectID]); } } return cout; } /// <summary> /// 打印字符串 /// </summary> /// <param name="str">字符串</param> public void Print(string str) { Console.Write(str); } } }一个小测试
using System; using System.Collections.Generic; using System.Text; namespace ObjectShow { public class Person : ICout { private int objectID; // 输出对象标识 private bool hasCreateInstance; // 是否已经创建实例 // Person类成员 private string name; public string Name { get { return name; } } // 构造函数 public Person() { hasCreateInstance = false; name = string.Empty; } public Person(string pName) : this() { name = pName; this.AddObjectToDictionary( this.name); } // 重载隐式转换 public static implicit operator int(Person prsn) { return prsn.ObjectID; } #region ICout 成员 public int ObjectID { get { return objectID; } } public void AddObjectToDictionary(string name) { if (!this.hasCreateInstance) { this.objectID = Cout.AddObject("Name:" + name); this.hasCreateInstance = true; } } #endregion } public class MainClass { public static Cout ConsoleOut = new Cout(); public static void Main() { Person me = new Person("ZhouFeng"); ConsoleOut = ConsoleOut >> me; Console.ReadKey(); } } }