使用下面的示例,是否有 Joshua Bloch 的防御性复制技术的优雅 Java 实现?nullChecking 确实是我认为的问题,但也许有一种更简单的方法来实现防御性复制。
public class Audit {
private Date dateCompleted;
...
public Audit() {
super();
}
//defensive copy of dateCompleted
public final Date getDateCompleted() {
if (dateCompleted != null){
return new Date(dateCompleted.getTime());
}else{
return null;
}
}
public final void setDateCompleted(Date dateCompleted) {
if (dateCompleted != null){
this.dateCompleted = new Date(dateCompleted.getTime());
}else{
this.dateCompleted = null;
}
}
...
}