May expose internal representation by incorporating reference to mutable object
可以通过合并对可变对象的引用来公开内部表示
This code stores a reference to an externally mutable object into the internal representation of the object. If instances are accessed by untrusted code, and unchecked changes to the mutable object would compromise security or other important properties, you will need to do something different. Storing a copy of the object is better approach in many situations.
此代码将对外部可变对象的引用存储到对象的内部表示中。 如果不受信任的代码访问实例,并且对可变对象的未经检查的更改会危及安全性或其他重要属性,则需要执行不同的操作。 在许多情况下,存储对象的副本是更好的方法。
** 正确方法如下,用它的clone方法获取副本进行操作。**
public Date getBirthday() { return birthday == null ? null : (Date) birthday.clone(); } public void setBirthday(Date birthday) { this.birthday = birthday == null ? null : (Date) birthday.clone(); }