【问题标题】:Queue object remains empty when items queued onto it队列对象在项目排队时保持为空
【发布时间】:2014-03-12 06:33:40
【问题描述】:

我有两个班级,ClubpartyClubextends通用Queue<E>并实现fitnessInterface

Queue<E> implements QueueInterface<E>

俱乐部

public class Club extends Queue<Bachelor> implements fitnessInterface{

   protected Queue<Bachelor> bachelorQ;

   public Club(){
       super();

       bachelorQ = new Queue<Bachelor>();
    }

public boolean Eligible(Bachelor bachelor){

       if(bachelor==null){throw new NullPointerException();}

       return true;
     }

@Override
public QueueInterface<Bachelor> enqueue(Bachelor bachelor){

       if(bachelor==null){
             throw new NullPointerException();
       else{
             bachelorQ.enqueue(bachelor); }

       return this; //the class queue
    }
}

聚会

public class Party extends Club{
       super();
    }

@Override
public boolean Eligible(Bachelor bachelor){

   if(bachelor==null){throw new NullPointerException();}

   if(bachelor.getPushups()>=25){return true;}

   return false;

   }
}

Bachelor 类扩展了 QueueInterface,并具有一个构造函数,该构造函数接受 Integer 作为参数,并具有 getPushUps() 方法等。

问题是当我创建一个Party 对象并尝试将单身汉拉尔夫或查理排入队列时,它仍然是空的。

我的通用 Queue 类完美运行。它有dequeueenqueueisEmptypeeksizetoString 方法。一切正常。

主要

public static void main(String[] args){

     Party bigParty = new Party();

     Bachelor jack,charlie,ralph;

     jack = new Bachelor(15);
     ralph = new Bachelor(27);
     charlie = new Bachelor(39);

     //Eligibility tests
     System.out.println(bigParty.Eligible(charlie));
     System.out.println(bigParty.Eligible(ralph));
     System.out.println(bigParty.Eligible(jack));

     System.out.println(bigParty.enqueue(ralph));
     System.out.println(bigParty.enqueue(charlie));
     System.out.println(bigParty.size());
     System.out.println(bigParty.bachelorQ.size());
}

输出

true
true
false
[]
[]
0
2

【问题讨论】:

    标签: java generics inheritance linked-list queue


    【解决方案1】:

    当您混合继承和组合时会发生这种情况。检查这个方法:

    @Override
    public QueueInterface<Bachelor> enqueue(Bachelor bachelor){
    
           if(bachelor==null){
                 throw new NullPointerException();
    
           bachelorQ.enqueue(bachelor); 
    
           return this; //the class queue
        }
    }
    

    您在bachelorQ 上调用enqueue(),但返回this。两者都是不同的引用,指向不同的对象。

    您应该返回bachelorQ,或者直接调用super.enqueue(bachelor)

    顺便说一句,既然您已经在扩展 Queue&lt;Bachelor&gt;,我认为那里不需要 bachelorQ 参考。你可以删除它。甚至更好,只是不要从Queue&lt;Bachelor&gt; 扩展。您应该尽可能选择组合而不是继承。

    【讨论】:

      【解决方案2】:

      我同意 Rohit Jain 所说的一切。但是,这里还有一个问题。在您的 enqueue 方法中,您已将内容包装在 if(bachelor==null) 块中。因此,如果单身汉为空,单身汉Q 只会将单身汉排入队列。我认为这不是您打算让它工作的方式。

      @Override
      public QueueInterface<Bachelor> enqueue(Bachelor bachelor){
      
             if(bachelor==null){
                   throw new NullPointerException();
      
             bachelorQ.enqueue(bachelor); 
      
             return this; //the class queue
          }
      }
      

      我建议您重写您的 Club 类,使其看起来更像这样:

      public class Club implements fitnessInterface
      {
          protected Queue<Bachelor> bachelors;
      
          public Club(){
              super();
              bachelors = new Queue<Bachelor>();
          }
      
          public boolean Eligible(Bachelor bachelor)
          {
              if(bachelor==null) throw new NullPointerException();
              return true;
           }
      
          @Override
          public QueueInterface<Bachelor> enqueue(Bachelor bachelor)
          {
              if(bachelor==null) throw new NullPointerException();
              bachelors.enqueue(bachelor); 
              return bachelors;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多