预览加载中,请您耐心等待几秒...
1/10
2/10
3/10
4/10
5/10
6/10
7/10
8/10
9/10
10/10

亲,该文档总共23页,到这已经超出免费预览范围,如果喜欢就直接下载吧~

如果您无法下载资料,请参考说明:

1、部分资料下载需要金币,请确保您的账户上有足够的金币

2、已购买过的文档,再次下载不重复扣费

3、资料包下载后请先用软件解压,在使用对应软件打开

C#运算符的重载运算符重载运算符重载classA { intx; publicintX { get{returnx;} set{x=value;} } //重载加操作符 publicstaticAoperator+(Aa,Ab) { Ac=newA(); c.x=a.x+b.x; returnc; } //重载减操作符 publicstaticAoperator-(Aa,Ab) { Ac=newA(); c.x=a.x-b.x; returnc; } } classB { staticvoidMain() { Aa=newA(); a.X=3; Ab=newA(); b.X=5; //使用加法 Ac=a+b; Console.WriteLine(c.X); //使用减法 c=a-b; Console.WriteLine(c.X); } }类别运算符重载-续运算符重载-续//重载一元运算符 publicstaticCoordinateoperator++(Coordinateop1) { op1.x++; op1.y++; returnop1; } publicstaticCoordinateoperator--(Coordinateop1) { op1.x--; op1.y--; returnop1; }运算符重载-续//重载二元运算符 publicstaticCoordinateoperator+(Coordinateop1,Coordinateop2){ Coordinateresult=newCoordinate(); result.x=op1.x+op2.x; result.y=op1.y+op2.y; returnresult; } publicstaticCoordinateoperator+(Coordinateop1,intop2){ Coordinateresult=newCoordinate(); result.x=op1.x+op2; result.y=op1.y+op2; returnresult; } publicstaticCoordinateoperator*(Coordinateop1,Coordinateop2){ Coordinateresult=newCoordinate(); result.x=op1.x*op2.x; result.y=op1.y*op2.y; returnresult; }运算符重载-续//重载关系运算符 publicstaticbooloperator>(Coordinateop1,Coordinateop2){ if(op1.x>op2.x&&op1.y>op2.y) { returntrue; } else { returnfalse; } } publicstaticbooloperator<(Coordinateop1,Coordinateop2){ if(op1.x<op2.x&&op1.y<op2.y) { returntrue; } else { returnfalse; } } publicstaticbooloperator==(Coordinateop1,Coordinateop2){ if(op1.x==op2.x&&op1.y==op2.y) { returntrue; } else { returnfalse; } } publicstaticbooloperator!=(Coordinateop1,Coordinateop2){ if(op1.x!=op2.x&&op1.y!=op2.y) { returntrue; } else { returnfalse; } }运算符重载-续//重载布林判断 publicstaticbooloperatortrue(Coordinateop1){ if(op1.x!=0||op1.y!=0) { returntrue; } else { returnfalse; } } publicstaticbooloperatorfalse(Coordinateop1){ if(op1.x==0&&op1.y==0) { returntrue; } else { returnfalse; } }运算符重载-续//重载逻辑运算符 publicstaticbooloperator!(Coordinateop1){ if(op1.x!=0&&op1.y!=0) { returnfalse; } else { returntrue; } } publicstaticbooloperator&(Coordinateop1,Coordinateop2){ if((op1.x!=0&&op1.y!=0)&(o