在Java中,如何调用具有不同变量类型的构造函数?

时间:2015-11-11 08:58:13

标签: java

我想这是一个简单的问题,但我刚刚开始学习! :P

我需要在checkParking()网关方法中调用ParkingTicket(parker,parkee)构造函数(如果" if"语句为true)但是我收到以下错误:不兼容的类型:int无法转换为Car

public class ParkingTicket
{
  private static int count = 1000;
  private final String referenceNumber;
  private final String carLicensePlate;
  private int carMinutesParked;
  private int meterMinutesPaid;
  private static final String prefix = "V";

  private ParkingTicket(String recordedLicense, int carParkedMinutes, int meterPaidMinutes)
  {
      count = (count + 1);
      referenceNumber = prefix + count;
      carLicensePlate = recordedLicense;
      carMinutesParked = carParkedMinutes;
      meterMinutesPaid = meterPaidMinutes;
  }

  private ParkingTicket(Car parker, ParkingMeter parkee)
  {
      this(parker.getLicensePlate(),
           parker.getMinutesParked(),
           parkee.getMinutesPaid());
  }

  public static int checkParking(Car minutesParked, ParkingMeter minutesPaid)
  {
    if (minutesParked.getMinutesParked() > minutesPaid.getMinutesPaid())
    {
       new ParkingTicket(minutesParked.getMinutesParked(), minutesPaid.getMinutesPaid());       
    }
    else
    {
       ParkingTicket pt1 = null;
    }
  }
} 

提前感谢所有帮助!

2 个答案:

答案 0 :(得分:2)

在线

Select area, book, sequence, accountnumber, name, address, polenumber from master
    where newconnectiondate is null
      OR newconnectiondate between '1950-01-01' and '2015-10-31'

您正在尝试使用构造函数

new ParkingTicket(minutesParked.getMinutesParked(), minutesPaid.getMinutesPaid());

正如您所看到的,这需要两个参数, private ParkingTicket(Car parker, ParkingMeter parkee) Car

您传递的参数是ParkingMeter

我猜你要调用第一个构造函数?您只需要更改该行以包含许可证

int

答案 1 :(得分:0)

您应该使用传递构造函数所期望的参数:

 new ParkingTicket(minutesParked.getLicensePlate(), minutesParked.getMinutesParked(), minutesPaid.getMinutesPaid());       

我会更改这些变量的名称。 ParkingTicket ticket = new ParkingTicket(minutesParked, minutesPaid); 不是minutesParked的好名字,而Car minutesPaid不是ParkingMeter的好名字。

P.S。你可能想对你创建的实例做些什么。