如何在Java

时间:2015-07-16 22:28:47

标签: java

我正在创建一个库存系统(这样做是为了好玩),我选择使用队列作为我在java中的数据结构。我在公共类中使用私有类,当我试图运行它时测试队列我不能声明私有类的变量。这是我的代码

包含私有类

的类
public class Customer
{//begining of class

    private class Custque //creating private class to use as node
   {
        private int    Trn; 
        private String Lname;
        private String Fname;
        private String Mname;
        private String Mstatus;
        private String DOB;
        private String Email;
        private String permanentadd;
        private String mailingadd;
        Custque Next;


          public  Custque()//default constructor
          {
            Trn=0;
            Lname="";
            Fname="";
            Mname="";
            Mstatus="";
            DOB="";
            Email="";
            permanentadd="";
            mailingadd="";
            this.Next= Next;

          }

          public Custque(int T, String Ln, String Fn,String Mn, String MS, String dob, String email, String PA, String MA)//primary constructor
          {
               Trn=T;
               Lname=Ln;
               Fname=Fn;
               Mname=Mn;
               Mstatus=MS;
               DOB=dob;
               Email=email;
               permanentadd=PA;
               mailingadd=MA;
           }

         public void setTrn(int T)//trn setter
         {
           this.Trn= T;

         }

         public int getTrn() // trn getter
         {
           return Trn; 
         }

        public void setLname(String Ln)
        {
          this.Lname=Ln;
        }

        public String getLname()
        {
         return Lname;
        }

        public void setFname(String Fn)
        {
         this.Fname=Fn;
        }

       public String getFname()
       {
        return Fname;
       }

       public void setMname(String Mn)
       {
        this.Mname=Mn;
       }

      public String getMname()
      {
         return Mname;
      }

      public void setMstatus(String Ms)
      {
        this.Mstatus=Ms;
      }

      public String getMstatus()
      {
       return Mstatus;
      }

     public void setDOB(String dob)
     {
      this.DOB=dob;
     }

      public String getDOB()
     {
       return DOB;
     }

     public void setEmail(String email)
     {
      this.Email=email;
     }

      public String getEmail()
      {
       return Email;
      }

       public void setpermanentadd(String PA)
       {
         this.permanentadd=PA;
       }

       public String getpermanentadd()
        {
          return permanentadd;
        }
       public void setMailingAdd(String MA)
       {
         this.mailingadd=MA;
       }

       public String getmailingAdd()
      {
       return mailingadd;
      }

 }// end of private class

 private Custque front;
 private Custque last;

 public void enqueue (int trn, String Ln, String Fn, String Mn, String MS,String dob,String email,String PA, String MA)
 {
     Custque que = new Custque();//  creating a variable for the queue is null
     if (que.equals(null))// testing if the queue is null
        System.out.println("An error has occur");

       que.setTrn(trn);
       que.setLname(Ln);
       que.setFname(Fn);
       que.setMname(Mn);
       que.setMstatus(MS);
       que.setDOB(dob);
       que.setEmail(email);
       que.setpermanentadd(PA);
       que.setMailingAdd(MA);

      if(front.equals(null))
        last=front=que;
      else
        last.Next = que; 

       last = que; 

 }// end of enqueue

 public Custque dequeue()
 {
     if (front.equals(null)) 
     {
         System.out.printf("Queue is Empty");
         return null;
     }

     Custque i;
     i=front;
     front = front.Next;

    return front ; 
 }// end of dequeue



 void display()
 {
      Custque d;
      if(front != null)
      {
         d=front;
         System.out.printf("" + d);
      }
 }


}//end of main class

我的司机班

import java.util.Scanner;


public class driver 
{

public static void main(String[] args)
{
        int t,i;
        String ln,fn,mn,ms,dob,email,pa,ma;
        Custque s;/* line of error*/

        for(i=0; i<5;i++)
        {
            System.out.printf("");
            Scanner num = new Scanner(System.in);
            t= num.nextInt();

            System.out.printf("");
            Scanner l = new Scanner(System.in);
            ln = l.nextLine();

            System.out.printf("");
            Scanner f = new Scanner(System.in);
            fn = f.nextLine();

            System.out.printf("");
            Scanner m = new Scanner(System.in);
            mn = m.nextLine();

            System.out.printf("");
            Scanner mar = new Scanner(System.in);
            ms = mar.nextLine();

            System.out.printf("");
            Scanner d = new Scanner(System.in);
            dob = d.nextLine();

            System.out.printf("");
            Scanner e = new Scanner(System.in);
            email = e.nextLine();

            System.out.printf("");
            Scanner per = new Scanner(System.in);
            pa = per.nextLine();

            System.out.printf("");
            Scanner mai = new Scanner(System.in);
            ma = mai.nextLine();


        }// end of loop

}

}//end of driver

1 个答案:

答案 0 :(得分:1)

私有内部类意味着您只能访问外部类,在本例中为Customer。 事实上,如果你宣布私人内部阶级,你自己会说:“我希望没有人能看到这个课程”。所以你违背了你的一个代码。

但是,就像@Hovercraft Full Of Eels所说,这背后的要求是什么。也许我们可以提供更多帮助或者只是对java语义的怀疑?