我无法理解它,我用单独的名称和值声明/实例化所有对象,如下所示:
// Initialise cars - null constructors and non-null constructors.
ParkedCar car1 = new ParkedCar();
ParkedCar car2 = new ParkedCar("BBB 222", "Toyota", "Echo", "Black", 200);
ParkedCar car3 = new ParkedCar("CCC 333", "Ford", "Taurus", "Silver", 100);
ParkedCar car4 = new ParkedCar("DDD 444", "Dodge", "Charger", "Blue", 500);
ParkedCar car5 = new ParkedCar("EEE 555", "Kia", "Rio", "Grey", 75);
//Initialise meters
ParkingMeter pm1 = new ParkingMeter(car1, 60);
ParkingMeter pm2 = new ParkingMeter(car2, 60);
ParkingMeter pm3 = new ParkingMeter(car3, 120);
ParkingMeter pm4 = new ParkingMeter(car4, 0);
ParkingMeter pm5 = new ParkingMeter(car5, 60);
(如果您好奇的话,这是其他类的构造函数:
public ParkedCar()
{
licensePlate = "AAA 111";
carMake = "Honda";
carModel = "Accord";
carColour = "Red";
minutesParked = 15;
}
public ParkedCar(String license, String carMa, String carMod, String carCol, int time)
{
carMake = carMa;
carModel = carMod;
carColour = carCol;
licensePlate = license;
minutesParked = time;
}
因此,目标是让 ParkedCar 对象每个对象都有一个 ParkingMeter 对象(只需在 ParkedCar 对象上设置 timePaid 变量),以及一个 PoliceOfficer 对象来检查“支付时间”与“ parking 时间”并发出 parking 票(实例化一个 ParkingTicket 对象),罚款金额与逾期 parking 时间相对应。我可以提供完整的类,但这是 ParkingTicket 类的相关部分,因为这是它的打印位置:
public ParkingTicket(ParkedCar car)
{
ParkedCar illegalParker = car;
licensePlate = illegalParker.getLicensePlate();
carMake = illegalParker.getCarMake();
carModel = illegalParker.getCarModel();
carColour = illegalParker.getCarColour();
minutesParked = illegalParker.getMinutesParked();
minutesPaid = illegalParker.getMinutesPaid();
fineAmount = ParkingTicket.getFineAmount(illegalParker);
badgeNumber = PoliceOfficer.getBadgeNumber();
officerName = PoliceOfficer.getOfficerName();
}
public static String getTicketText(ParkedCar car, PoliceOfficer issuer)
{
PoliceOfficer ticketIssuer = issuer;
ParkedCar illegalParker = car;
ticketText = "PARKING TICKET $"+ParkingTicket.getFineAmount(illegalParker)+".\n"
+"License Plate: "+illegalParker.getLicensePlate()+".\n"
+"Car Description: "+illegalParker.getCarColour()+" "+illegalParker.getCarMake()+" "+illegalParker.getCarModel()+".\n"
+"Issued by: "+ticketIssuer.getOfficerName()+", Badge Number: "+ticketIssuer.getBadgeNumber()+".\n";
return ticketText;
}
public String toString()
{
return "PARKING TICKET $"+this.fineAmount+".\n"
+"License Plate: "+this.licensePlate+".\n"
+"Car Description: "+this.carColour+" "+this.carMake+" "+this.carModel+".\n"
+"Issued by: "+this.officerName+", Badge Number: "+this.badgeNumber+".\n";
}
无论我使用 .toString() 方法直接打印对象还是打印 .getTicketText() 方法返回的字符串都没关系...
为什么所有 ParkedCar 对象的值都设置为 car5 的值?
请随意索取更多代码片段,因为此作业的目的是演示类之间的连接性,并且正在进行大量来回引用。
最佳答案
static
字段由所有实例共享。删除静态
private String carMake; // <-- not static
private String carModel; // <-- not static
private String carColour; // <-- not static
private String licensePlate; // <-- not static
private int minutesParked; // <-- not static
这样每个实例都有自己的实例。
JLS-8.3.1.1. static
Fields说(部分),
If a field is declared
static
, there exists exactly one incarnation of the field, no matter how many instances (possibly zero) of the class may eventually be created. A static field, sometimes called a class variable, is incarnated when the class is initialized (§12.4).A field that is not declared
static
(sometimes called a non-static
field) is called an instance variable. Whenever a new instance of a class is created (§12.5), a new variable associated with that instance is created for every instance variable declared in that class or any of its superclasses.
关于java - 我实例化的一个类的所有对象都使用与实例化的该类型的最后一个对象相同的值打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28270132/