代码之家  ›  专栏  ›  技术社区  ›  user7554125

重写泛型equals方法

  •  0
  • user7554125  · 技术社区  · 8 年前

    我试图在我正在制作的程序中实现一个通用队列,但在某一点上被卡住了。我制作的程序应该是模拟一台打印机,排队等待打印作业。有一个Queue类、PrintQueue类和job类。 我包含了一个函数(在printQueue类中),如果第一个作业与您输入的作业ID匹配,它将被删除。

    然而,不幸的是,队列是通用的。这意味着我不能用一个整数遍历数组来检查等式

    工作类别

    public class Job{
        private String owner;
        private int jobId;
    
        public Job(String o, int j){
            owner = o;
            jobId = j;
        }
        public String getOwner(){
            return owner;
        }
        public int getJobId(){
            return jobId;
        }
        public String toString() {
            return owner + "    " + jobId + ".  ";
        }
    
        public boolean equals(Job a) {
            if(this.jobId == a.getJobId() || this.owner.equals(a.getOwner())) {
                return true;
            }
            else
                System.out.println("nomatch");
                return false;
        }
    }
    

    通用队列类

       import java.util.ArrayList;
       public class Queue<T>{
        private ArrayList<T> queue;
        public Queue() {
            queue = new ArrayList<T>();
        }
        public void enQueue(T obj1) {
            queue.add(obj1);
        }
        public T deQueue() {
            if(queue.size() != 0) {
                T temp = queue.get(queue.size() - 1);
                queue.remove(queue.size() -1);
                return temp;
            }
            else
                return null;
        }
        public int size() {
            return queue.size();
        }
        public boolean isEmpty() {
            if (size() == 0) {
                return true;
            }
            else
                return false;
        }
        public int positionOf(T a) {
            for(int x = 0; x < queue.size(); x++) {
                if(a.equals(queue.get(x))) {
                    System.out.println("Positionmatch");
                    return x;
                }
            }
            return -1;
        }
    }
    

    PRINTQUEUE类

    public class PrintQueue {
        Queue<Job> prqueue = new Queue<Job>();
        public PrintQueue() {}
    
        public void lprm(int jobID) { //Removes the active job at the front of the queue if jobId matches, error message otherwise
            //I can't JUST use jobID to check the position because the queue is a collection of JOBS not JobId's
            if (prqueue.positionOf(new Job("",jobID))==0) {
                prqueue.deQueue();
            }
            else if (prqueue.positionOf(new Job("",jobID))== -1) {
                System.out.println("Job does not occupy first row.");
            }
        }
    }
    

    我知道这是一个涉及面很广的问题,所以如果你愿意花时间阅读,非常感谢。如果我能在其他地方找到答案,我不会问这个问题。

    2 回复  |  直到 8 年前
        1
  •  1
  •   Shadov    8 年前

    解决方法很简单:你们并没有在你们的类中重写equals,这是一个常见的错误。始终使用注释方法 @Override 所以你可以避免这个错误。

    equals 方法是采取 Object 参数,并且您的 Job

    如果你在使用IDE,我建议 right click -> source -> generate equals 你们会看到一个很好的例子。

        2
  •  0
  •   François LEPORCQ    8 年前

    @Override
    public boolean equals(Object a) {
        if(!(a instanceof Job))
            throw new IllegalArgumentException();
        Job job =(Job)a;
        if(this.jobId == job.getJobId() || this.owner.equals(job.getOwner())) {
            return true;
        }
        else
            System.out.println("nomatch");
        return false;
    }
    

    另请参见 Why do I need to override the equals and hashCode methods in Java?