gpt4 book ai didi

java - 返回对象的新实例

转载 作者:行者123 更新时间:2023-11-30 03:38:33 27 4
gpt4 key购买 nike

我正在编写一个程序来模拟 parking 罚单系统。总共有 4 个不同的类,分别是 ParkedCar、ParkingMeter、ParkingTicket 和 PoliceOfficer。在我的 PoliceOfficer 类中,我有一个 if 语句来确定警察是否应该为停放的汽车开出罚单。

我有以下声明来执行此操作:返回新的 ParkingTicket(this,car,meter)。我收到类型不兼容的错误。如果我需要发布其他类(class)的代码,请告诉我。

parking 票等级:

import java.text.DecimalFormat;

public class ParkingTicket {
private static int ticketCount;
private ParkedCar car;
private PoliceOfficer officer;
private ParkingMeter meter;
private double fine;

public final double BASE_FINE = 25.0;
public final double HOURLY_FINE = 10.0;

public ParkingTicket (ParkedCar aCar, PoliceOfficer anOfficer, ParkingMeter aMeter) {
this.car = aCar;
this.officer = anOfficer;
this.meter = aMeter;

calculateFine();
}


public void calculateFine() {
// Calculate fine & increment ticket count
++ticketCount;

int timeOver = (int)Math.ceil((car.getMinutesParked() - meter.getMinutesPurchased() / 60.0));
fine = BASE_FINE * HOURLY_FINE * (timeOver - 1);
}

public String toString() {
DecimalFormat newDecimal = new DecimalFormat("$###.00");
return "Ticket Number: " + ticketCount + "\n" +
car + "\n" + meter + "\n" + "Fine: " + newDecimal.format(fine) + "\n"
+ officer;
}
}

警察级别:

public class PoliceOfficer {
// Declare data fields
private String name;
private String badgeNumber;

// Default constructor
public PoliceOfficer (String name, String badgeNumber) {
this.name = name;
this.badgeNumber = badgeNumber;
}

// Copy of PoliceOfficer
public PoliceOfficer (PoliceOfficer OfficerCopy) {
this.name = OfficerCopy.name;
this.badgeNumber = OfficerCopy.badgeNumber;
}

// Class to check a parked car and determine if the
// cars time has expired
public ParkingTicket check(ParkedCar car, ParkingMeter meter) {

if (car.getMinutesParked() > meter.getMinutesPurchased()) {
// Creating a new object of ParkingTicket object
return new ParkingTicket(this,car,meter);
} else {
return null;
}
}

// toString class
public String toString() {
return "Officer Name: " + name + "/n" + "Badge ID: " + badgeNumber;
}
}

最佳答案

你刚刚颠倒了你的论点。 ParkingTicket 构造函数要求其参数为:

(ParkedCar, PoliceOfficer, ParkingMeter)

但是你已经通过了

(PoliceOfficer, ParkedCar, ParkingMeter)

您所需要做的就是将调用更改为new ParkingTicket(car, this,meter)

关于java - 返回对象的新实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27298485/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com