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. 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. (I'm not done with my code, I just started. The integers x
and y
were just to try to make it work.)
我现在正在使用eclipse来编写我的Java程序,但是我需要覆盖equals方法,因此它将获取实际数据,而不是名称或id。当我尝试按照我所知的方式自动生成它时,它说我没有非静态变量。我添加了一些,它仍然无法正常工作。我不太了解Java自己做的事情,但我知道我很有可能理解你在说什么。 (我没有完成我的代码,我刚开始。整数x和y只是为了让它工作。)
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();
}
}
2 个解决方案
#1
0
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. So if you changed your example to the following, you could implement those methods (or have them auto-generated for you by eclipse):
您拥有“没有非静态变量”的消息正在为您提供正确的提示。覆盖hashCode和equals方法只有在类中存在非静态变量时才有意义。因此,如果您将示例更改为以下内容,则可以实现这些方法(或者通过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. 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).
请注意我是如何将x和y变量移动到类级别而不是在您拥有它们的方法级别。还请注意,创建者或创建者是创建者,而不是创建者,我不建议命名包mainPackage,并且我不会为了每个类而在每个类中导入java.util。*它(如果你正在使用eclipse,只需按Ctrl + Shift + O组织你的导入)。
Please also see which issues to consider when overriding these methods.
还请查看覆盖这些方法时要考虑的问题。
#2
-1
To automatically add equals
and hashCode
methods:
要自动添加equals和hashCode方法:
- Menu -> Source -> Generate hashCode() and equals()
- Select the fields you want make part of hashCode() and equals()
- Click Ok
菜单 - >源 - >生成hashCode()和equals()
选择你想要成为hashCode()和equals()的一部分的字段
The rest will be done by eclipse.
其余的将由eclipse完成。
#1
0
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. So if you changed your example to the following, you could implement those methods (or have them auto-generated for you by eclipse):
您拥有“没有非静态变量”的消息正在为您提供正确的提示。覆盖hashCode和equals方法只有在类中存在非静态变量时才有意义。因此,如果您将示例更改为以下内容,则可以实现这些方法(或者通过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. 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).
请注意我是如何将x和y变量移动到类级别而不是在您拥有它们的方法级别。还请注意,创建者或创建者是创建者,而不是创建者,我不建议命名包mainPackage,并且我不会为了每个类而在每个类中导入java.util。*它(如果你正在使用eclipse,只需按Ctrl + Shift + O组织你的导入)。
Please also see which issues to consider when overriding these methods.
还请查看覆盖这些方法时要考虑的问题。
#2
-1
To automatically add equals
and hashCode
methods:
要自动添加equals和hashCode方法:
- Menu -> Source -> Generate hashCode() and equals()
- Select the fields you want make part of hashCode() and equals()
- Click Ok
菜单 - >源 - >生成hashCode()和equals()
选择你想要成为hashCode()和equals()的一部分的字段
The rest will be done by eclipse.
其余的将由eclipse完成。