我想在C#中重载赋值运算符=,但是这是不允许的,有什么其他的算符可以表达成赋值的意思,但又能够重载么?
xiexie
4 个解决方案
#1
并不是所有的操作符都可以重载,下面的操作符才可以重载
一元操作符 + - ! ~ ++ -- true false
二元操作符 + - * / % & | ^ << >> == != > < >= <=
转换操作符 隐式转换()和显式转换()
赋值操作符不可以重载。
一元操作符 + - ! ~ ++ -- true false
二元操作符 + - * / % & | ^ << >> == != > < >= <=
转换操作符 隐式转换()和显式转换()
赋值操作符不可以重载。
#2
所以请大家给各替代的运算符,能够代表这个意思就行了。
#3
小弟不会,但帮你up
#4
定义的时候重载操作符方法必须是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();
}
}
}
以下是程序代码,有些地方写得不完善,只能说仅仅说明了问题:
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();
}
}
}
#1
并不是所有的操作符都可以重载,下面的操作符才可以重载
一元操作符 + - ! ~ ++ -- true false
二元操作符 + - * / % & | ^ << >> == != > < >= <=
转换操作符 隐式转换()和显式转换()
赋值操作符不可以重载。
一元操作符 + - ! ~ ++ -- true false
二元操作符 + - * / % & | ^ << >> == != > < >= <=
转换操作符 隐式转换()和显式转换()
赋值操作符不可以重载。
#2
所以请大家给各替代的运算符,能够代表这个意思就行了。
#3
小弟不会,但帮你up
#4
定义的时候重载操作符方法必须是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();
}
}
}
以下是程序代码,有些地方写得不完善,只能说仅仅说明了问题:
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();
}
}
}