Java 如何将在主类中创建的对象与在另一个类中创建的对象一起使用?

Java 如何将在主类中创建的对象与在另一个类中创建的对象一起使用?,java,Java,你好,我是java新手,我想做的是,我在我的主类LinkedStackDemo中创建了一个堆栈,对象如下所示LinkedStack s=new LinkedStack()稍后在我的LinkedStack类中,我创建了另一个堆栈,该堆栈从我在主类上创建的堆栈中获取一定数量的元素。我的问题是,当我将第二个堆栈传递给LinkedStack类中的一个方法时,我希望能够不断弹出第一个堆栈(主堆栈)中的元素,但当我尝试弹出时,它只会弹出我正在传递的第二个堆栈中的元素 以下是主要程序: public clas

你好,我是java新手,我想做的是,我在我的主类LinkedStackDemo中创建了一个堆栈,对象如下所示
LinkedStack s=new LinkedStack()稍后在我的LinkedStack类中,我创建了另一个堆栈,该堆栈从我在主类上创建的堆栈中获取一定数量的元素。我的问题是,当我将第二个堆栈传递给LinkedStack类中的一个方法时,我希望能够不断弹出第一个堆栈(主堆栈)中的元素,但当我尝试弹出时,它只会弹出我正在传递的第二个堆栈中的元素
以下是主要程序:

public class LinkedStackDemo
  {
   public static void main (String a[])
   {
    LinkedStack  s = new LinkedStack();

    LinkedQueue  q = new LinkedQueue();

    s.push(100,1.35);
    s.push(50,1.40); 
    s.push(40,1.45);

    System.out.println("Size of the stack: " + s.size());

    System.out.println();   
    System.out.println("STACK contains following items till this moment:"); 
    s.print(); 
    System.out.println();

    s.reciept(200);
    System.out.println();
    s.push(250, 1.60);


     System.out.println();

    System.out.println("STACK contains following items till this moment:");
    s.print();



  }
}
链接堆栈类

    class LinkedStack 
       {
       private Node head; //the first node
       private int size; // number of items
       private LinkedQueue  q = new LinkedQueue();
       private int check=0;
       //nested class to define node
       private static class Node
       { 
         double item;
         double price;
         Node next;


       }

       //Zero argument constructor
       public LinkedStack()
       {
         head = null;
         size = 0;
       }

       public boolean isEmpty()
       {
         return (size == 0);
       }

       //Remove item from the beginning of the list.
       public Node pop()
       {
         if(isEmpty()){
             System.out.println("Stack is empty");
         }
         Node current= head;

         head = head.next;
         size--;
         check-=current.item;
         return current;

       }

       //Add item to the beginning of the list.
       public void push(double item, double price)
       {
         Node oldHead = head;
         head = new Node();
         head.item = item;
         head.price= price;
         head.next = oldHead;
         size++;
         check+=item;
         if(!q.isEmpty()){
          q.CalMissOrders(check,q);
         }

       } 

       public int size()
       {
         return size;
       }

       public void print(){
           Node current= head;
           while(current!=null){
               System.out.println("Widgets "+current.item+" Price                                     $"+current.price);
               current=current.next;
           }

       }

       public void printReciept(LinkedStack s, double order){
           Node current;
           double totalPrice=0;
           double bookPrice=0;
           System.out.println("Reciept:");
           while(!s.isEmpty()){
               current=s.pop();
               totalPrice=current.price;
               bookPrice+=current.item*current.price;
                System.out.println("Widgets "+current.item+" price of each          $"+current.price+" the total is $"+current.item*current.price);
           }
          System.out.println("Ordered Widgets "+order+" price of each $"+         (totalPrice+(totalPrice*.4))+" Total plus 40% = $"+Math.round((totalPrice+(totalPrice*.4))*order) );
          System.out.println("Total cost = $"+bookPrice+" for the bookKeeper’s records.");
      }

       public void reciept(double order){
           LinkedStack  s = new LinkedStack();
           double count =0;     
           Node mrecently;
             while(!isEmpty()&&count<order){ 
                mrecently=pop();
                count+=mrecently.item; //adding widgets
                 if(count>order){
                     double extraWidgets; 
                     extraWidgets=count-order;
                     push(extraWidgets,mrecently.price);
                     s.push(mrecently.item-extraWidgets, mrecently.price);
                     break;
                 }

                s.push(mrecently.item, mrecently.price);         
            }
             if(count<order){
                 double miss=order-count;

                 q.enQ(s,order,miss);

                 System.out.println("We are out of widgets your order will          be placed in a queue until next shipment");
             }
             else{
                 printReciept(s,order);
             }
       }

       public  void MissOrders(double missing,LinkedStack s,double order){   
           double count =0;     
           Node mrecently;
           System.out.println("test1");

           s.print();
             while(!isEmpty()&&count<missing){ 
                mrecently=pop();
                count+=mrecently.item; //adding widgets
                 if(count>missing){
                     double extraWidgets; 
                     extraWidgets=count-missing;
                     push(extraWidgets,mrecently.price);
                     s.push(mrecently.item-extraWidgets, mrecently.price);
                     break;
                 }

                s.push(mrecently.item, mrecently.price); 

            }
             System.out.println("test2");
             s.print();
             s.printReciept(s,order);

       }

     }  
Receipt方法工作得很好,它从主类中创建的堆栈中弹出元素,并将其推送到此处创建的堆栈中。如果我有足够的项目,它将打印一个receipt如果没有新的堆栈将被保存,并等待更多的元素被推到第一个堆栈。
错序法给我带来了麻烦。当我尝试从主堆栈类中弹出元素时,它不允许我这样做。它从im中弹出,传递在Receipt方法中创建的那个。任何建议谢谢您的时间。

我没有看到任何调用
receipt()
方法的代码。你能给我看一下这个方法的调用吗?是在我的主方法
s.receipt(200)上吗它只是询问我需要在第二个堆栈@Soumitri Pattnaik中弹出和推送的项目数量。你可以用完整的类更新你的
LinkedStack
类代码吗?@Soumitri Pattnaik它现在在那里。很抱歉。
      import java.util.*;
      public class LinkedQueue{

      private int total;
      private Node front, rear;

      private static class Node {
         static  LinkedStack ele;
          double miss;
          double order;
          Node next;
      }

      public LinkedQueue() {
          front = null;
          rear = null;
          total = 0;
      }

      public void enQ(LinkedStack s, double order, double missing)
      {
          Node current = rear;
          rear = new Node();
          rear.ele = s;
          rear.order=order;
          rear.miss=missing;

          if (total++ == 0) front = rear;
          else current.next = rear;


      }

      public Node deQ()
      {
          if (total == 0) throw new java.util.NoSuchElementException();
          Node current = front;
          front = front.next;
          if (--total == 0) rear = null;
          return current;
      }
       public Node peek()
      {
          if (isEmpty() )
              throw new NoSuchElementException("Underflow Exception");
          return front;
      }    
       public void print(){
        Node current= front;
        int pos=0;
        while(current!=null){

          LinkedStack  s;
          s=current.ele;
          s.print(); 

          System.out.println("Position in Queue "+(++pos)+" Order Widgets: "+current.order+" Missing Widgets "+current.miss);
          current=current.next;
        }

       }
       public void CalMissOrders(int check, LinkedQueue q){
           Node current=q.front;
            if(check>=current.miss){
                current=q.deQ();
               LinkedStack s= current.ele;
               double miss=current.miss;
               double order=current.order;
               s.MissOrders(miss,s,order);
            }
       }


       public boolean isEmpty() {
          return (front == null);
      }
       public int getSize()
      {
          return total;
      }    

  }