如何检索Java中通过参数接收的对象中的数据?

如何检索Java中通过参数接收的对象中的数据?,java,object,instance-variables,Java,Object,Instance Variables,我试图通过修改以前实现的接受泛型的队列来构建优先级队列。我创建了一个名为Priority Queue(接受泛型)的类,它扩展了我的队列类(接受泛型)。最后一个类是主要焦点,Queue。我在测试中推送一个客户,当我在队列的推送方法中收到该客户时,我想用它来比较其优先级与队列中另一个客户的优先级。任何关于这方面的建议都会有帮助,但我的问题是,如何从队列内部访问对象客户(或推送中的val)的3个字段 我有一个测试类,测试我的比较器是否工作。我还设置了一个测试用例,以便在实现优先级队列时确保它工作 用于

我试图通过修改以前实现的接受泛型的队列来构建优先级队列。我创建了一个名为Priority Queue(接受泛型)的类,它扩展了我的队列类(接受泛型)。最后一个类是主要焦点,Queue。我在测试中推送一个客户,当我在队列的推送方法中收到该客户时,我想用它来比较其优先级与队列中另一个客户的优先级。任何关于这方面的建议都会有帮助,但我的问题是,如何从队列内部访问对象客户(或推送中的val)的3个字段

我有一个测试类,测试我的比较器是否工作。我还设置了一个测试用例,以便在实现优先级队列时确保它工作

用于测试比较器是否工作以及优先级队列在实现时是否工作的类:

public class TestCustomer {

   public static void main(String args[]) {

   Customer customer1 = new Customer(3000, 20, 5);
   Customer customer2 = new Customer(5000, 15, 7);
   Customer customer3 = new Customer(5000, 20, 5);
   Customer customer4 = new Customer(3000, 3, 5);
   Customer customer5 = new Customer(3000, 3, 8);


   // comparator test

   Customer.WorthComparator worth = new Customer.WorthComparator();
   Customer.LoyaltyComparator loyal = new Customer.LoyaltyComparator();
   Customer.WorthPoliteComparator polite = new Customer.WorthPoliteComparator();

   assert worth.compare(customer1, customer2) == -1;  
   assert worth.compare(customer2, customer3) == 0;  
   assert worth.compare(customer2, customer1) == 1; 

   assert loyal.compare(customer1, customer2) == 1; 
   assert loyal.compare(customer2, customer1) == -1; 
   assert loyal.compare(customer1, customer3) == 0; 

   assert polite.compare(customer3, customer2) == -1; 
   assert polite.compare(customer2, customer3) == 1; 
   assert polite.compare(customer1, customer2) == -1; 
   assert polite.compare(customer2, customer3) == 1; 
   assert polite.compare(customer1, customer4) == 0;

   // priority queue test

   PriorityQueue<Customer> pQueueW = new PriorityQueue<Customer>(worth); 
   PriorityQueue<Customer> pQueueL = new PriorityQueue<Customer>(loyal);
   PriorityQueue<Customer> pQueueP = new PriorityQueue<Customer>(polite); 



   // push -- type T, pass in a val // judgement upon worth

   //PUSH customers for Worth
   pQueueW.push(customer1);
   pQueueW.push(customer2);
   pQueueW.push(customer4);
   assert pQueueW.pop() == customer2;



   //PUSH customers for Loyalty
   pQueueL.push(customer1);
   pQueueL.push(customer2);
   pQueueL.push(customer3);
   assert pQueueL.pop() == customer1;


   //PUSH customers for Polite
   pQueueP.push(customer2);
   pQueueP.push(customer4);
   pQueueP.push(customer5);
   assert pQueueP.pop() == customer2;
   assert pQueueP.pop() == customer5;


   //























   }

   }
公共类TestCustomer{
公共静态void main(字符串参数[]){
客户1=新客户(3000,20,5);
客户2=新客户(5000,15,7);
客户3=新客户(5000,20,5);
客户4=新客户(3000,3,5);
客户5=新客户(3000,3,8);
//比较试验
Customer.worthcomarator-worth=新客户.worthcomarator();
Customer.LoyaltyComparator忠诚度=新Customer.LoyaltyComparator();
Customer.WorthPoliteComparator polite=新Customer.WorthPoliteComparator();
assert-worth.compare(customer1、customer2)=-1;
assert-worth.compare(customer2、customer3)=0;
assert worth.compare(customer2,customer1)==1;
断言忠诚。比较(customer1,customer2)==1;
断言忠诚。比较(customer2,customer1)=-1;
断言忠诚。比较(customer1、customer3)==0;
表示礼貌。比较(customer3,customer2)=-1;
表示礼貌。比较(customer2,customer3)==1;
表示礼貌。比较(customer1,customer2)=-1;
表示礼貌。比较(customer2,customer3)==1;
断言礼貌。比较(customer1,customer4)==0;
//优先级队列测试
PriorityQueue PQUEW=新的PriorityQueue(价值);
PriorityQueue pQueueL=新的PriorityQueue(忠诚);
PriorityQueue PQUEP=新的PriorityQueue(礼貌);
//push——键入T,传入val//判断值
//向顾客推销价值
产品推送(客户1);
产品推送(客户2);
产品推送(客户4);
断言pquew.pop()==customer2;
//推动客户忠诚
pqueel.push(客户1);
pqueel.push(客户2);
pqueel.push(客户3);
断言pQueueL.pop()==customer1;
//向顾客推销礼貌
PQUEP.push(客户2);
PQUEP.push(客户4);
PQUEP.push(客户5);
断言pquep.pop()==customer2;
断言pquep.pop()==customer5;
//
}
}
我的Priority Queue类只是扩展了队列并使用了它的push函数。我需要创建一个方法来调用我的pop in队列:

import java.util.Comparator;

public class PriorityQueue<T> extends Queue<T>
{

   Comparator<T> compare;

   public PriorityQueue(Comparator<T> comp)
   {
      compare = comp;
   }


    //@Override
   public void push(T val)
   {
       super.push(val); //right now this is just a normal Queue as it will do what its parent did.
   }
import java.util.Comparator;
公共类优先级队列扩展了队列
{
比较器比较;
公共优先级队列(比较器comp)
{
比较=比较;
}
//@凌驾
公共无效推送(T val)
{
super.push(val);//现在这只是一个普通队列,因为它将执行其父队列所执行的操作。
}
我的customer类有一个构造函数来创建客户。这也是我实现不同比较器来创建客户排序的地方:

import java.util.Comparator;

public class Customer
{

   int netWorth;
   int yearsWithCompany;
   int politeness;


   public Customer(int netWorth,int yearsWithCompany,int politeness)
   {
      this.netWorth = netWorth;
      this.yearsWithCompany = yearsWithCompany;
      this.politeness = politeness;
   }


/**
   compares clients based on thier net worth
*/
   public static class WorthComparator implements Comparator<Customer>
   {




   */
      public int compare(Customer c1, Customer c2)
      {
         int net1 = c1.netWorth;
         int net2 = c2.netWorth;
         if (net1 == net2) {
            return 0;
            }
         else if (net1 < net2) {
            return -1;
            }
         else {
            return 1;
            }
      }

   }


/**
   compares clients based on thier loyalty 
*/
   public static class LoyaltyComparator implements Comparator<Customer>
   {

   /**



   */
      public int compare(Customer c1, Customer c2)
      {

         int years1 = c1.yearsWithCompany;
         int years2 = c2.yearsWithCompany;

         if (years1 == years2) {
            return 0;
            }
         else if (years1 < years2) {
            return -1;
            }
         else {
            return 1;
            }

      }

   }



/**
   compares clients based on thier net worth.
   If there is a tie, politeness is used.
*/
   public static class WorthPoliteComparator implements Comparator<Customer>
   {

   /**

   */
      public int compare(Customer c1, Customer c2)
      {
         if (c1.netWorth == c2.netWorth)
        {
         if (c1.politeness < c2.politeness) {
            return -1;
         }        
         else if (c1.politeness > c2.politeness) {
            return 1;
         }
         else {
            return 0;
            }
         }
        else if (c1.netWorth > c2.netWorth){
         //int politeness = WorthComparator.compare(c1, c2);
         //return politeness;
         return 1;
          }

        else {
         return -1 ;
         }


         }

   }

}
import java.util.Comparator;
公共类客户
{
国际网络;
国际年瑞士公司;
礼貌;
公共客户(国际网络公司、国际年度公司、国际礼貌)
{
this.netWorth=netWorth;
this.yearsWithCompany=yearsWithCompany;
礼貌=礼貌;
}
/**
根据客户的资产净值进行比较
*/
公共静态类Comparator实现Comparator
{
*/
公共整数比较(客户c1、客户c2)
{
int net1=c1.netWorth;
int net2=c2.netWorth;
如果(net1==net2){
返回0;
}
否则如果(net1c2.礼貌){
返回1;
}
否则{
返回0;
}
}
否则如果(c1.网络>c2.网络){
//int礼貌=worthcomarator.compare(c1,c2);
//回敬礼貌;
返回1;
}
否则{
返回-1;
}
}
}
}
我的队列类已实现为常规队列。我现在正在修改它,以便将其转换为优先级队列。队列类如下所示: 公共类队列 {

公共类QNode{
专用QNode;
私人旅行社;
公共QNode(QNode节点,T val){
this.node=节点;
this.val=val;
}
}
受保护的QQ节点头;
保护QNode后方;
受保护的QNode温度;
公众的
   public class QNode<T> {

      private QNode<T> node;
      private T val;  

      public QNode(QNode<T> node, T val) {
         this.node = node;
         this.val = val;

      }
   }

   protected QNode<T> head;
   protected QNode<T> rear;
   protected QNode<T> temp;




   public Queue()
   {
      head = null;
      rear = null;

         }

   public void push(T val) // T = Customer type -- val is a customer
   {
      // if I wanted to get the contents out of val, which is a customer
      // who has a net worth, years loyal, and politeness
      // how would I access let's say, the netWorth from val?

      // first node created
      if (head == null && rear == null){
         head = new QNode<T>(rear, val);
         rear = head;

      }


   }

   public T pop()
   {
      if (head == null){
         throw new QueueUnderFlowException();
      }

      else if(head == rear) {
        T temp_hold = head.val;
        head = null;
        rear = null;
        return temp_hold;

      }

      else {

         T oldN = head.val;

         this.head = this.head.node;
         return oldN;      
         }
      }

    /**
      returns true if the queue is empty
     */

   public boolean isEmpty()
   {
      if (head == null) {
       return true;
   }
      else {
       return false;
      }
   }

}
public class Queue<T extends Comparable<T>> {
    // code...
public class Customer implements Comparable<Customer> {
    @Override
    public int compareTo(Customer other) {
        return Integer.compare(netWorth, other.netWorth);
    }

    // rest of the code...
}