Java封装的数组变异器方法

Java封装的数组变异器方法,java,arrays,constructor,encapsulation,Java,Arrays,Constructor,Encapsulation,我有一个任务,我正试图编码,但不幸的是,这给了我一个困难的时间 我浏览了互联网和课本,但似乎找不到这种特殊困境的例子 基本上,我需要为火车编写一个预订引擎,我们得到了我们想要使用的启动代码,基本上写出了我们的方法,并将它们插入到适当的类中 主要的问题是,我们需要将包含火车票对象的主数组封装在一个单独的类中,并基本上编写mutator和accessor方法。需要与数组进行任何交互,以便在不需要访问时保持数组的不可访问性和安全性 这是程序的驱动程序类 Private static void menu

我有一个任务,我正试图编码,但不幸的是,这给了我一个困难的时间

我浏览了互联网和课本,但似乎找不到这种特殊困境的例子

基本上,我需要为火车编写一个预订引擎,我们得到了我们想要使用的启动代码,基本上写出了我们的方法,并将它们插入到适当的类中

主要的问题是,我们需要将包含火车票对象的主数组封装在一个单独的类中,并基本上编写mutator和accessor方法。需要与数组进行任何交互,以便在不需要访问时保持数组的不可访问性和安全性

这是程序的驱动程序类

Private static void menuAdd() 


{
       String  passName,op1,op2;
       int seatNum;
       Boolean FCOption,waiter,indicator;
       int duration;
       char fClass,wService;

   System.out.print("Please Enter a seat number :");
   seatNum = stdin.nextInt();
   stdin.nextLine();

   System.out.print("Please Enter the passenger name :");
   passName = stdin.nextLine();
   System.out.print(passName);

   System.out.print("Please Enter number of legs for this trip :");
   duration = stdin.nextInt();

   System.out.println("Would you like to consider a First Class ticket for an additional $20 per leg? :");
   System.out.print("Please enter Y/N");
   op1 = stdin.next();
   fClass =op1.charAt(0);

   stdin.nextLine();
   System.out.print("Would you like to consider a waiter service for a flat $15 Fee?");
   System.out.print("Please enter Y/N");
   op2 = stdin.next();
   wService =op2.charAt(0);


   //Now we create the ticket object

   TrainTicket ticketx = new TrainTicket(seatNum,passName,duration);

   System.out.println("This is an object test printing pax name"+ticketx.getName());

   TicketArray.add(ticketx);
}

因此,基本上,我可以编写代码,请求用户提供各种详细信息,然后使用TrainTicket对象的构造函数调用来安装该对象, 当我使用

TicketArray.add(ticketx);
eclipse告诉我“无法从TicketArray类型对非静态方法add(TrainTicket)进行静态引用”

这就是array类的外观

    Public class TicketArray
{
   // ..............................................
   // .. instance variables and constants go here ..
   // ..............................................
    int counter ;
    int arraySize =100 ;

   // constructor
   public TicketArray()
   {
      // ....................
      // .. implement this ..
      // ....................
       TrainTicket [] tickets =new TrainTicket[arraySize];
   }

   // add() method:
   // take the passed in TrainTicket object and attempt to store it in the
   // data structure. If the structure is full, or the seat of the given
   // TrainTicket has already been booked, the operation should return
   // false; otherwise return true.

   public boolean add(TrainTicket data)
   {
      // ....................
      // .. implement this ..
      // ....................

       tickets[counter]=data;
      // dummy return value so the skeleton compiles
      return false;
   }
知道它为什么不起作用吗? 如果有人能解释如何以这种方式封装数组,我将不胜感激。我熟悉构造函数的工作方式及其方法的编写,但出于某种原因,我发现很难对数组做同样的事情


提前感谢。

这里的问题不是mutator或accessor方法,甚至不是数组,而是您在尝试使用
TicketArray
类之前没有创建它的实例
add(Ticket t)
被定义为一个实例方法,这意味着您需要有一个
TicketArray
的实例才能添加到它

试试这个:

//create a new Ticket
TrainTicket ticketx = new TrainTicket(seatNum,passName,duration);

//create a new Ticket Array
TicketArray tarr = new TicketArray();
tarr.add(ticketx);

您需要一个实例才能调用实例方法。这个问题与数组无关。嗨,Dave,你能澄清一下吗?什么的实例?TicketArray,您试图调用实例方法的类。或者,您可以将其设置为静态方法,但是ew,IMO.ok,所以一旦我实例化TicketArray,并使用tarr.add(ticketx)传递ticket对象,如何在public boolean add(TrainTicket data)方法中将对象添加到数组中?@user1405824这与您的方法类似。您需要跟踪数组中的当前索引,然后在向数组添加元素时,将索引增加1。