我实例化其中一个类的所有对象都使用与实例化该类型的最后一个对象相同的值进行打印

时间:2015-02-02 01:55:49

标签: java

我无法理解,我声明/实例化所有具有不同名称和值的对象,如下所示:

// 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对象来检查"支付时间" vs"停放时间"并发出停车票(实例化ParkingTicket物件),罚款相当于他们已经停车多久。我可以提供完整的课程,但这里是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";
}

通过直接打印对象或打印.getTicketText()方法返回的String,我是否使用.toString()方法并不重要......

为什么所有的ParkedCar对象都是'值被设置为car5的值

随意请求更多的代码片段,因为这个分配的目的是演示类之间的连接,并且来回进行大量的引用。

2 个答案:

答案 0 :(得分:5)

所有实例共享

static个字段。删除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

这样每个实例都会得到它的&#39;自己的。

JLS-8.3.1.1. static Fields说(部分),

  

如果一个字段被声明为static,那么无论该类最终可以创建多少个实例(可能为零),都只存在该字段的一个化身。初始化类(§12.4)时,会生成静态字段(有时称为类变量)。

     

未声明为static的字段(有时称为非static字段)称为实例变量。每当创建一个新的类实例(§12.5)时,就会为该类或其任何超类中声明的每个实例变量创建一个与该实例关联的新变量。

答案 1 :(得分:0)

问题是你的方法是静态的。基本上,当方法或变量是静态的时,它将在类的所有实例中共享。因此,每次调用静态函数都是一样的。我想你可能想要考虑你提到的功能是否应该是静态的。