[英]I can't seem to figure out why I keep getting true when I clearly overridden the equality method
As I try to setup the code whenever I run it the result would end up becoming True when I need it to produce a false test as well.当我尝试在每次运行代码时设置代码时,当我需要它来生成错误测试时,结果最终会变为 True。 Extensive testing shows it to be always true and I have no idea how to produce a false on it.
广泛的测试表明它总是正确的,我不知道如何在它上面产生错误。
import java.util.Scanner;
public class LandTract
{
// instance variables
private static double length , width, area;
/**
* Constructor for objects of class LandTract
*/
public LandTract(double length, double width, double area)
{
// initialise instance variables
length = 0;
width = 0;
}
public LandTract(double length, double width)
{
this.length = length;
this.width = width;
}
public void setLength(double length)
{
this.length = length;
}
public double getLength()
{
return length;
}
public void setWidth(double width)
{
this.width = width;
}
public double getWidth()
{
return width;
}
public double getArea()
{
return area = length * width;
}
public String toString()
{
String str = "Length: " + length + "\nWidth: " + width;
return str;
}
public boolean equals(Object obj)
{
LandTract land = (LandTract) obj;
if (this.length != land.length)
return false;
if (this.width != land.width)
return false;
if (this.area != land.area)
return false;
return true;
}
public static void main(String[] args)
{
Scanner key = new Scanner(System.in);
System.out.print("Enter the length of the first tract of land: ");
length = key.nextDouble();
key.nextLine();
System.out.print("Enter the width of the first tract of land: ");
width = key.nextDouble();
key.nextLine();
LandTract land1 = new LandTract(length , width);
System.out.println("The area of the first tract of land is " + land1.getArea());
System.out.println();
System.out.print("Enter the length of the second tract of land: ");
length = key.nextDouble();
key.nextLine();
System.out.print("Enter the width of the second tract of land: ");
width = key.nextDouble();
key.nextLine();
LandTract land2 = new LandTract(length, width);
System.out.println("The area of the second tract of land is " + land2.getArea());
System.out.println();
if (land1.equals(land2))
System.out.println("Both tracts of land are the same size.");
else
System.out.println("They are different sizes.");
}
}
The best example for a confusing & ironically erroneous comment:令人困惑且具有讽刺意味的错误评论的最佳示例:
// instance variables
private static double length , width, area;
The program works much better, when you:当您:
(Really) Introduce instance variables: (真的)引入实例变量:
private double length, width, area;
Fix compiler problems in main method (by declaring local variables with the same identifier..no good style but quick):修复 main 方法中的编译器问题(通过使用相同的标识符声明局部变量..没有好的风格但很快):
public static void main(String[] args) { double length, width; //... }
The problem here is that the values being compared ( length
, width
, and area
) are static fields, not instance fields.这里的问题是要比较的值(
length
、 width
和area
)是 static 字段,而不是实例字段。 This means that any reference to them will use the same global value, regardless of which instance of the class is referencing them.这意味着对它们的任何引用都将使用相同的全局值,无论 class 的哪个实例正在引用它们。
Of particular relevance, this.length.= land.length
in the equals
method will always return true
, since both this.length
and land.length
will refer to the same value.特别相关的是,
equals
方法中的this.length.= land.length
将始终返回true
,因为this.length
和land.length
将引用相同的值。 (Note that this guarantee is no longer true if multiple threads are involved, but that's not the case with this example.) (请注意,如果涉及多个线程,则此保证不再适用,但本示例并非如此。)
This also means that any call to a constructor or a setter will set the shared static fields, overwriting the value previously written when calling a setter or constructor on another instance.这也意味着对构造函数或 setter 的任何调用都将设置共享的 static 字段,从而覆盖之前在另一个实例上调用 setter 或构造函数时写入的值。 For instance, the
length, width
constructor will overwrite the static length
& width
fields, and the setLength
method will overwrite the static length
field.例如,
length, width
构造函数将覆盖 static length
和width
字段,而setLength
方法将覆盖 static length
字段。
public LandTract(double length, double width)
{
this.length = length;
this.width = width;
}
public void setLength(double length)
{
this.length = length;
}
The fix is to change these fields to instance fields, rather than static ones:修复方法是将这些字段更改为实例字段,而不是 static 字段:
public class LandTract
{
private double length, width, area;
// [...]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.