转自:http://www.cnblogs.com/hyddd/articles/1391118.html
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.
这个问题和Inconsistent synchronization描述的问题很类似,解决方案也很类似,可以参考看看:http://www.cnblogs.com/hyddd/articles/1391098.html
先看一段代码:
public class Test extends Thread{
public static void main(String args[]) throws Exception{
Test3 obj = new Test3();
Date now = new Date();
obj.setRegDate(now);
now.setYear(4000); //问题所在!
System.out.println(obj.getRegDate());
}
}
public class Test3 {
private Date regDate ;
public void setRegDate(Date regDate) {
this.regDate = regDate;
}
public Date getRegDate() {
return regDate;
}
}
这段代码的输出是:Thu Feb 15 21:47:13 CST 5900
如果main里面不加now.setYear(4000);这句代码呢,结果是:Sun Feb 15 21:47:31 CST 2009
从这里我们发现了,修改一个对象,可能会引起其他对象的修改,因为JAVA里,对象是引用传递的......所以这里我的建议是:setObj的时候,对象不要直接赋值(this.regDate = regDate),而是赋值传入对象的拷贝(this.regDate = (Date)regDate.clone();)。
OK~现在我们把代码this.regDate = regDate替换成this.regDate = (Date)regDate.clone();,运行一下看看结果,噢,输出是:Sun Feb 15 21:47:31 CST 2009。
分享到:
相关推荐
May expose internal representation by incorporating reference to mutable object 描述:调用set方法,修改对象属性,被修改的对象属性是一个可变的对象; May expose internal representation by returning ...
NULL 博文链接:https://futeng.iteye.com/blog/1833449
May expose internal representation by incorporating reference to mutable object 描述:调用set方法,修改对象属性,被修改的对象属性是一个可变的对象; May expose internal representation by returning ...
Connecting to Elasticsearch by Using Spring Data 30.6.3. Spring Data Elasticsearch Repositories 30.7. Cassandra 30.7.1. Connecting to Cassandra 30.7.2. Spring Data Cassandra Repositories 30.8. ...
ADO seems to be the ideal way to expose tabular data from your own COM objects and the ATL OLE DB Provider templates can help!使用ADO导出数据
cryptography is a package designed to expose cryptographic.zip
this document does not provide you with any legal rights to any intellectual property in any microsoft product you may copy and use this document for your internal reference purposes ó 2013 ...
It will also cover what's been added to the new Spring Boot 2 release, including Spring Framework 5 features like WebFlux, Security, Actuator and the new way to expose Metrics through Micrometer ...
FreeRTOS的官方文档, 描述了FreeRTOS的很多技术细节(FreeRTOS User Guide, expose many details of FreeRTOS)
expose模板wordpress 外国收费模板,
mr2 - Expose local TCP and UDP server to external network USAGE: mr2 [global options] command [command options] [arguments...] VERSION: 20210401 COMMANDS: server Run as server mode client Run as ...
USING OR REGISTERING TO USE THE LICENSED MATERIALS, YOU ARE AGREEING TO BE BOUND BY THE TERMS OF THIS EULA, INCLUDING, BUT NOT LIMITED TO, OWNERSHIP AND GRANT OF LICENSE TERMS IN SECTIONS 2 AND 4,...
Designed to be both a comprehensive reference and a practical cookbook, the text is divided into the following three parts: Part I, Overview, gives high-level descriptions of the hardware and ...
The goal of this guide is to manage this complexity by describing in detail the dos and don'ts of writing C++ code. These rules exist to keep the code base manageable while still allowing coders to ...
It will also cover what's been added to the new Spring Boot 2 release, including Spring Framework 5 features like WebFlux, Security, Actuator and the new way to expose Metrics through Micrometer ...
需要node版本v14.16.1(其他版本可能有效,但未经测试), npm , clang (带有clang++ ), gnuplot (用于覆盖图), make和python2 (作为path中的python)。 在Ubuntu上,由于默认情况下已删除python2,因此我...
o prototype an algorithm or design in AWK to get it running quickly and expose problems early. Often, the interpreted performance is adequate and the AWK prototype becomes the product.
Through a review of recent Web application breaches, the book will expose the prolific methods hackers use to execute Web attacks using common vulnerabilities such as SQL Injection, Cross-Site ...
Twitter Digg Facebook Del.icio.us Reddit Stumbleupon Newsvine Technorati Mr....Add to your CodeProject bookmarks ...In v2.3 a single simple change of converting internal classes ...