简体   繁体   English

无法使用Eclipse自动生成equals方法

[英]Can't auto-generate an equals method with eclipse

I'm using eclipse to write my Java programs now, but I need to override the equals method so it will take the actual data and not the name or id. 我现在使用eclipse编写Java程序,但是我需要重写equals方法,这样它将使用实际数据,而不是名称或id。 When I try to auto-generate it the way I know, it says I have no non-static variables. 当我尝试以自己知道的方式自动生成它时,它表示我没有非静态变量。 I added some in and it still doesn't work. 我添加了一些,但仍然无法正常工作。 I don't know enough about Java to do it myself, but I know enough that I would most likely understand what you are talking about. 我对Java不够了解,无法自己做,但我了解得最多,我很可能会理解您在说什么。 (I'm not done with my code, I just started. The integers x and y were just to try to make it work.) (我还没有完成我的代码,我才刚开始。整数xy只是试图使其工作。)

package mainPackage;
import java.util.*;

public class Main extends Creater {
    public static void main(String[] args) {
        int x = 0;
        int y = 0;
        thatInput = Inputs.ask();
        Loops.CreateArray();
    }
}

The message that you have "no non-static variables" is giving you the right hint. 消息“没有非静态变量”正在为您提供正确的提示。 Override the hashCode and equals methods only makes sense if there are non-static variables in the class. 仅当类中包含非静态变量时,才重写hashCodeequals方法才有意义。 So if you changed your example to the following, you could implement those methods (or have them auto-generated for you by eclipse): 因此,如果将示例更改为以下示例,则可以实现这些方法(或通过Eclipse为您自动生成它们):

public class Main extends Creater {
    private int x = 0;
    private int y = 0;

    public static void main(String[] args) {
        // other code
    }

    @Override
    public boolean equals(Object obj) {
        // ... your equals code goes here
    }

    @Override
    public int hashCode() {
        // ... your hashCode, er, code goes here
    }

    // ... other code that does wonderful things with x and y
}

Note how I've moved your x and y variables to be at the class level rather than at the method level where you had them. 请注意,我如何将xy变量移到类级别而不是您拥有它们的方法级别。 Please also note that someone who or something that creates is a creator , not a creater , that I wouldn't recommend naming your package mainPackage , and that I wouldn't go importing java.util.* in every class just for the sake of it (if you're using eclipse, just do Ctrl + Shift + O to organise your imports). 请注意谁或什么, 创造某人是一个创造者 ,而不是一个创世 ,我不会建议命名你包mainPackage ,而且我也不会去进口java.util.*在每个班只为求(如果您使用的是eclipse,则只需按Ctrl + Shift + O即可组织导入)。

Please also see which issues to consider when overriding these methods . 另请参见覆盖这些方法时要考虑的问题

To automatically add equals and hashCode methods: 自动添加equalshashCode方法:

  1. Menu -> Source -> Generate hashCode() and equals() 菜单->源->生成hashCode()和equals()
  2. Select the fields you want make part of hashCode() and equals() 选择要成为hashCode()和equals()一部分的字段
  3. Click Ok 点击确定

The rest will be done by eclipse. 其余的将通过蚀来完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM