简体   繁体   English

Java 将不同 class 中的 void 方法调用到主 class?

[英]Java calling a void method in a different class to a main class?

I have a method to issue a parkingTicket in my officer class,我有一种方法可以在我的官员 class 中发出停车票,

public ParkingTicket issueParkingTicket(ParkedCar car,ParkingMeter meter){

        if(isParkingTimeExpired(car,meter) == true){
          ParkingTicket ticket = new ParkingTicket(officerName,officerBadgeNumber,car.getLicensePlateNumber(),calculateFine(car,meter));
          ticket.displayDetails();


          return ticket;
            } else
             { return null;
    }           
   }

I was asked to modify it in a way to have it not return anything so i made it void, so I did it this way我被要求以某种方式对其进行修改以使其不返回任何内容,因此我将其设为无效,所以我这样做了

 public void issueParkingTicket(ParkedCar car,ParkingMeter meter){

        if(isParkingTimeExpired(car,meter) == true){
          ParkingTicket ticket = new ParkingTicket(officerName,officerBadgeNumber,car.getLicensePlateNumber(),calculateFine(car,meter));

          ticketList.add(ticket);
          ticket.displayDetails();
    }

Now in my main driver class, I have to created an officer object but since I had to make the method void to not return anything I am getting an error saying void cannot be converted to ParkingTicket , if i take away myCar1,myMeter1 from the parentheses, I get an error telling me there's arguments required ParkedCar, ParkingMeter.现在在我的主要驱动程序 class 中,我必须创建一个官员 object 但由于我必须使方法 void 不返回任何东西,我收到一个错误,说 void 不能转换为ParkingTicket ,如果我从括号中myCar1,myMeter1 ,我收到一条错误消息,告诉我有 arguments 需要 ParkedCar、ParkingMeter。 How can I make it so I don't get an error and the officer object created can issue a parking ticket?我怎样才能做到这一点,这样我就不会出错,并且创建的警官 object 可以发出停车罚单?

 public class Driver
{
    main method
    public static void main(String [] args){

ParkedCar myCar1 = new ParkedCar("Fred","toyota",2013,"Z1234",25);
    myCar1.displayDetails();

    ParkingMeter myMeter1 = new ParkingMeter("SYDNEY",true,0.15,70); 
    myMeter1.displayDetails();


    PoliceOfficer officer1 = new PoliceOfficer("John doe","DMX1234"); 
    ParkingTicket ticket = officer1.issueParkingTicket(myCar1,myMeter1); 

This is the source of my error ParkingTicket ticket = officer1.issueParkingTicket(myCar1,myMeter1);这是我的错误ParkingTicket ticket = officer1.issueParkingTicket(myCar1,myMeter1);的来源

You can just remove the part that says:您可以删除显示以下内容的部分:

ParkingTicket ticket = 

in your main method.在你的主要方法中。

Since the ticket is created in the function there is no need to create a new ticket when you call the function.由于工单是在 function 中创建的,因此在调用 function 时无需创建新工单。

Add an array or List to ParkedCar that keeps track of all the tickets for that vehicle.向 ParkedCar 添加一个数组或列表,以跟踪该车辆的所有票。

public void issueParkingTicket(ParkedCar car, ParikingMeter meter)
{
   if(isParkingTimeExpired(car,meter) == true){
      ParkingTicket ticket = new ParkingTicket(officerName,officerBadgeNumber,car.getLicensePlateNumber(),calculateFine(car,meter));
  car.tickets.add(theTicket);

} }

then in your main class you can access every ticket for any car object.然后在您的主要 class 中,您可以访问任何汽车 object 的每张票。

public static void main(String [] args)
{
  ParkedCar car = new ParkedCar(/*params*/);
  Officer officer1 = new Officer(/*params*/);
  officer1.issueTicket(/*params*/);

  //make a getter for the tickets in the ParkedCar class
  car.getTickets();
}

You can pass in a ticket, and manipulate the ticket inside the issueParkingTicket method.您可以传入一张票,并在 issueParkingTicket 方法中操作该票。

ParkingTicket ticket = new ParkingTicket();
officer1.issueParkingTicket(myCar1,myMeter1,ticket); 
ticket.displayDetails();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM