假设我有一个Car 类,这是它的代码:
public class Car {
private String make;
private String model;
private int year;
public Car()
{
this.make = "";
this.model = "";
this.year = 0;
}
public Car(Car c)
{
this.make = c.getMake();
this.model = c.getModel();
this.year = c.getYear();
}
public String getMake() {
return make;
}
public String getModel() {
return model;
}
public int getYear() {
return year;
}
/* Trouble here */
public Car copy(Car c)
{
return c; // But needs all properties to be same as current instance of class.
}
}
请注意,我的私有字段没有 Setter 方法。有没有一种Copy(Car c)
方法可以将我的实例复制到相同类型的目标对象中并返回目标对象?
不添加 Setters 方法。