Java中的对象修改

时间:2018-12-06 02:03:09

标签: java oop object methods constructor

给出以下对象构造函数:

public class Reservation {

 private int roomNum;
 private int numPeople;
 private int endTime;

 public Reservation() {
 this.roomNum = 100;
 this.numPeople = 100;
 }

 public Reservation(int roomNum, int endTime) {
 this.roomNum = roomNum;
 this.numPeople = 30;
 this.endTime = endTime;
 }

还有物体

Reservation a = new Reservation();
Reservation c = new Reservation(309, 12);

如果我设置

a = c; 
a.addTime(2) // adds number of hours to endTime

a.endTime12的{​​{1}}变更还是会影响14?如果是这样,为什么?

1 个答案:

答案 0 :(得分:1)

a =c之后,绑定到的原始对象已丢失,将被垃圾回收。 a与c拥有相同的对象。