我需要澄清 Object 的 equal 方法中的 String[] 比较。下面是我的代码快照。当我执行这段代码时,它给出的是真假而不是真假。同样在我的情况下,任务类为每个调用生成唯一的 id。我究竟做错了什么?任何建议将不胜感激。
public class JobTask extends Task {
String[] args;
public WFTask(String[] args) {
this.args = args;
}
@Override
public boolean equals(Object obj) {
if (! ( obj instanceof String[] ) ){
return false;
}
String[] arguments = (String[] ) obj;
boolean ret = Arrays.equals(this.args , arguments );
return ret;
}
@Override
public void execute(TaskExecutionContext context) throws RuntimeException {
--------------
--------------
}
public static void main(String[] args) {
String[] arg1 = {"a","b","c"};
String[] arg2 = {"a","b","c"};
System.out.println( Arrays.equals(arg1,arg2));
JobTask t1 = new JobTask(arg1);
JobTask t2 = new JobTask(arg2);
System.out.println( t1.equals(t2));
}
}
public abstract class Task {
private Object id = GUIDGenerator.generate();
public Task() {
}
Object getId() {
return id;
}
public abstract void execute(TaskExecutionContext context) throws RuntimeException;
}