C#中奇妙的操作符重载
2024-07-21 02:19:31
供稿:网友
c#中奇妙的操作符重载
细心的朋友可能发现,c#虽然可以重载操作符,但和c++比较起来,却有很大的不同。定义的时候重载操作符方法必须是static,而且至少有一个参数(一目和二目分别是一个和两个),c#和c++比起来,最重要的特征是:<、>;==、!=;true、false必须成对出现,即重载了“<”就必须重载“>”,重载了“==”就必须重载“!=”,重载了“true”就必须重载“false”;另外,还必须实现基类object的两个虚方法:gethashcode()和equals(object obj)。
以下是程序代码,有些地方写得不完善,只能说仅仅说明了问题,不对之处请指教:
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.firstname.compareto(n2.firstname)>0 && n1.lastname.compareto(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.firstname.compareto(n2.firstname)<0 && n1.lastname.compareto(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();
}
}
}