简体   繁体   English

当我清楚地覆盖平等方法时,我似乎无法弄清楚为什么我一直保持真实

[英]I can't seem to figure out why I keep getting true when I clearly overridden the equality method

I'm trying to figure this out but I can't seem to get it to compare correctly.我试图弄清楚这一点,但我似乎无法正确比较它。

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:当您:

  1. (Really) Introduce instance variables: (真的)引入实例变量:

     private double length, width, area;
  2. 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.这里的问题是要比较的值( lengthwidtharea )是 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.lengthland.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 lengthwidth字段,而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.

相关问题 我似乎无法找出为什么我不断收到错误“无效的方法声明;需要返回类型” - I can't seem to find out why I keep getting the error "Invalid method declaration; return type required" 在第 16 行从二维数组输入中获取 InputMismatchException 并且我似乎无法弄清楚为什么 - Getting An InputMismatchException On Line 16 From A 2D Array Input And I Can't Seem To Figure Out Why 我不知道为什么我得到StackOverFlowError - I can't figure out why I'm getting StackOverFlowError 无法弄清楚为什么我不断收到找不到符号编译错误 - Can't figure out why i keep getting cannot find symbol compile error 绘图程序:不断出现错误,我不知道为什么 - Drawing program: Keep getting errors and I can't figure out why 我一直在试图弄清楚如何纠正这个错误,但我似乎无处可去 - I've been trying to figure out how to correct this error and i can't seem to be getting anywhere 无法弄清楚为什么我得到了StringIndexOutOfBoundsException - Can't figure out why I'm getting a StringIndexOutOfBoundsException 无法弄清楚为什么我得到空值 - Can't figure out why I am getting null values 无法弄清楚为什么我的方法异常 - Can't figure out why i'm getting an exception for my method 您好,我似乎无法弄清楚为什么我的程序无法正常工作 - Hello I can't seem to figure out why my program isn't working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM