不用while、for写死循环。
来源:互联网 发布:白人 狐臭 知乎 编辑:程序博客网 时间:2024/04/30 09:13
今天终于又写了一死循环程序。深深的陷入了操作符重载这个陷阱。
代码如下:
public static bool operator ==(Line line1, Line line2)
{
if (line1 == null && line2 == null)
return true;
if (line1 != null && line2 != null)
return line1.Equals(line2);
return false;
}
public static bool operator !=(Line line1, Line line2)
{
if (line1== null && line2==null)
return false;
if (line1 != null && line2 != null)
return !line1.Equals(line2);
return true;
} 这就是个不折不扣的死循环,而且是死得很惨的那种。
如果你在某个地方使用了如下形式的代码,不死循环都很难。
代码如下:
public class Line : ICloneable
{
private Point2D endPoint;
private Point2D startPoint;
private Line()
: this(Point2D.Empty, Point2D.Empty)
{
}
public Line(Point2D startPoint, Point2D endPoint)
{
this.startPoint = startPoint;
this.endPoint = endPoint;
}
public object Clone()
{
return new Line(this.startPoint, this.endPoint);
}
public override bool Equals(object obj)
{
if (obj != null)
{
if (!(obj is Line))
return false;
Line line = (Line)obj;
if (this.startPoint.Equals(line.startPoint))
return this.endPoint.Equals(line.endPoint);
}
return false;
}
public static bool operator ==(Line line1, Line line2)
{
if (line1 == null && line2 == null)
return true;
if (line1 != null && line2 != null)
return line1.Equals(line2);
return false;
}
public static bool operator !=(Line line1, Line line2)
{
if (line1== null && line2==null)
return false;
if (line1 != null && line2 != null)
return !line1.Equals(line2);
return true;
}
public static Line Empty
{
get
{
return new Line(Point2D.Empty, Point2D.Empty);
}
}
[Browsable(false)]
public bool IsEmpty
{
get
{
return startPoint.IsEmpty ? endPoint.IsEmpty : false;
}
}
}
这个一个描述一根线条的类,这段代码怎么编译都不会出错。但请大家留意中间的这段代码:{
private Point2D endPoint;
private Point2D startPoint;
private Line()
: this(Point2D.Empty, Point2D.Empty)
{
}
public Line(Point2D startPoint, Point2D endPoint)
{
this.startPoint = startPoint;
this.endPoint = endPoint;
}
public object Clone()
{
return new Line(this.startPoint, this.endPoint);
}
public override bool Equals(object obj)
{
if (obj != null)
{
if (!(obj is Line))
return false;
Line line = (Line)obj;
if (this.startPoint.Equals(line.startPoint))
return this.endPoint.Equals(line.endPoint);
}
return false;
}
public static bool operator ==(Line line1, Line line2)
{
if (line1 == null && line2 == null)
return true;
if (line1 != null && line2 != null)
return line1.Equals(line2);
return false;
}
public static bool operator !=(Line line1, Line line2)
{
if (line1== null && line2==null)
return false;
if (line1 != null && line2 != null)
return !line1.Equals(line2);
return true;
}
public static Line Empty
{
get
{
return new Line(Point2D.Empty, Point2D.Empty);
}
}
[Browsable(false)]
public bool IsEmpty
{
get
{
return startPoint.IsEmpty ? endPoint.IsEmpty : false;
}
}
}
public static bool operator ==(Line line1, Line line2)
{
if (line1 == null && line2 == null)
return true;
if (line1 != null && line2 != null)
return line1.Equals(line2);
return false;
}
public static bool operator !=(Line line1, Line line2)
{
if (line1== null && line2==null)
return false;
if (line1 != null && line2 != null)
return !line1.Equals(line2);
return true;
}
如果你在某个地方使用了如下形式的代码,不死循环都很难。
Line line1 = new Line();
Line line2 = new Line();
if (line1==line2)
{
}
要想解决上面的问题,必须把代码改成如下形式: Line line2 = new Line();
if (line1==line2)
{
}
public static bool operator ==(Line line1, Line line2)
{
if (object.Equals(line1, null) && object.Equals(line2, null))
return true;
if (!object.Equals(line1, null) && !object.Equals(line2, null))
return line1.Equals(line2);
return false;
}
public static bool operator !=(Line line1, Line line2)
{
if (object.Equals(line1, null) && object.Equals(line2, null))
return false;
if (!object.Equals(line1, null) && !object.Equals(line2, null))
return !line1.Equals(line2);
return true;
}
不过这里line是一个class,如果是struct,不知会这么样。还没试过呢。{
if (object.Equals(line1, null) && object.Equals(line2, null))
return true;
if (!object.Equals(line1, null) && !object.Equals(line2, null))
return line1.Equals(line2);
return false;
}
public static bool operator !=(Line line1, Line line2)
{
if (object.Equals(line1, null) && object.Equals(line2, null))
return false;
if (!object.Equals(line1, null) && !object.Equals(line2, null))
return !line1.Equals(line2);
return true;
}
- 不用while、for写死循环。
- shell死循环(while&&for)
- For , while , do…while的死循环.
- c语言中不用while,for,do-while实现循环
- 1118 while循环比for更容易导致死循环
- 如何写好while和for循环
- Perl while 死循环
- while死循环
- while 死循环
- while 和 for 构成的死循环的区别!!
- while(1)和for(;;)两个死循环有什么区别
- 不用循环 if for while 求1 。。。 n 之和
- for循环while循环
- while 死循环->小坑
- 【C语言天天练(一)】while(1)死循环与for(;;)死循环的区别
- while循环、do..while 循环、for循环
- for循环,do....while循环,while循环
- for循环,while循环,do while循环
- Linux系统调用跟我学(3)――僵尸进程
- (经典)生成java编制的可执行文件exe
- [Oracle]高效的PL/SQL程序设计(二)--标量子查询
- symbian试图框架结构
- MFC程序和Win32程序的关系
- 不用while、for写死循环。
- WPF中添加菜单里最前面的图标.
- 读取XML文件
- 【treeview开发笔记(3)】速度问题告一段落
- Spring AOP 面向方面
- 清清楚楚 Symbian60系统错误代码详解
- 自定义Url重写(支持ajax.net/atlas方案)
- Eclipse中常用的快捷键
- 用JSP+Servlet实现二进制图像的动态显示