比较 2 点 - java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19710455/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Comparing 2 Points - java
提问by Aaron Priesterroth
I've noticed that you can't compare 2 points like this:
我注意到你不能像这样比较两点:
if (pointOne == pointTwo) { }
I always have to do it like this:
我总是必须这样做:
if (pointOne.x == pointTwo.x && pointOne.y == pointTwo.y) { }
I really wonder why you can not use the first example, does anyone have an answer?
我真的想知道为什么你不能使用第一个例子,有人有答案吗?
采纳答案by neutrino
You must use the equals
method of class Point
. Check this.
您必须使用equals
class的方法Point
。检查这个。
If you use == what you are actually doing is checking if the memory address of the two Point
objects is the same.
如果你使用 == 你实际上在做的是检查两个Point
对象的内存地址是否相同。
In Java, all classes are derived from Object
, and you can override the equals
method, providing a convenient way of checking if in fact, two objects of the same Object
derived class, are the same.
在 Java 中,所有类都派生自Object
,您可以覆盖该equals
方法,从而提供一种方便的方法来检查同一Object
派生类的两个对象实际上是否相同。
回答by Sachin Kainth
Well this will be an object comparison. You would be comparing memory locations if you compare using ==.
那么这将是一个对象比较。如果您使用 == 进行比较,您将比较内存位置。
You could override and call Equals.
您可以覆盖并调用 Equals。
回答by Subhrajyoti Majumder
==
operator checks both reference pointing to the same object or not.
==
运算符检查两个引用是否指向同一个对象。
You should use equals
method of Point
object -
你应该使用对象的equals
方法Point
-
pointOne.equals(pointTwo);
Determines whether or not two points are equal. Two instances of Point2D are equal if the values of their x and y member fields, representing their position in the coordinate space, are the same.
判断两点是否相等。如果表示它们在坐标空间中的位置的 x 和 y 成员字段的值相同,则 Point2D 的两个实例相等。
回答by Diego Catalano
Because the Point is reference, and if you need to use equals, you need to override the method equal. Java doest not support override operators like "==".
因为Point是引用,如果需要使用equals,就需要重写equal方法。Java 不支持像“==”这样的覆盖操作符。
回答by Blady
I assume pointOne
and pointTwo
are objects of some class? You cannot overload operators in java, that is why you have to compare fields.
In such cases it is a good practive to override the equals method for your class and use it this way:
我假设pointOne
并且pointTwo
是某个类的对象?您不能在 Java 中重载运算符,这就是您必须比较字段的原因。在这种情况下,重写类的 equals 方法并以这种方式使用它是一个很好的做法:
PointOne.equals(PointTwo)
回答by k2col
You can use your first example because running pointOne.x == pointTwo.x
compares the primitive types (int) which the == operator is capable of.
您可以使用第一个示例,因为运行pointOne.x == pointTwo.x
比较 == 运算符能够执行 的基本类型 (int)。
When you run pointOne == pointTwo
you're comparing the Point object references, which they're not.
当您运行时,pointOne == pointTwo
您正在比较 Point 对象引用,而它们不是。
As others have said, you can use pointOne.equals(pointTwo)
正如其他人所说,您可以使用 pointOne.equals(pointTwo)