转自: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。
分享到:
相关推荐
2. **使用不可变对象**:如果可能,使用不可变对象(如`String`)作为内部表示。不可变对象一旦创建就不能被修改,因此不会引起上述问题。 3. **提供安全的访问方法**:为外部代码提供修改内部表示的安全接口,如`...
- **May expose internal representation by incorporating reference to mutable object**:调用set方法,修改对象属性,被修改的对象属性是一个可变的对象。 - **描述**:这通常意味着通过公共接口暴露了内部状态...
May expose internal representation by incorporating reference to mutable object 描述:调用set方法,修改对象属性,被修改的对象属性是一个可变的对象; May expose internal representation by returning ...
JQuery Tools系列中的Expose是一个非常实用的工具,它允许用户通过半透明层突出显示页面上的某个部分。这项技术通常用于突出页面上的某些信息或者在用户操作时提示用户注意特定内容。以下是对JQuery Tools Expose...
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 ...
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导出数据
【expose模板WordPress详解】 WordPress是一款全球广泛应用的内容管理系统(CMS),以其开源、免费和高度可定制性著称。"expose"是针对WordPress设计的一款专业主题模板,它源自国外并带有收费性质,这意味着该模板...
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 ...
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 ...
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 ...
The current system might put too much pressure on copyright holders, who struggle to monitor vast amounts of user-generated content, or it may expose video sharing websites to potential legal risks ...
需要node版本v14.16.1(其他版本可能有效,但未经测试), npm , clang (带有clang++ ), gnuplot (用于覆盖图), make和python2 (作为path中的python)。 在Ubuntu上,由于默认情况下已删除python2,因此我...
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 ...
在这一背景下,"my expose"应运而生,它借鉴了Mac OS中的Exposé功能,旨在解决Windows用户在桌面管理上遇到的问题。 "My expose"的设计理念十分明确,即通过提供一个直观且高效的方式来展示和管理打开的窗口。在...
包含以下java源文件: ...com.google.gson.annotations.Expose.class com.google.gson.annotations.SerializedName.class com.google.gson.annotations.Since.class com.google.gson.annotations.Until.class ...
《cryptography:加密技术在Python中的实践》 在信息技术领域,数据安全至关重要,而加密技术则是保障信息安全的重要手段。在Python编程环境中,有一个名为`cryptography`的库,专门用于提供各种加密算法和安全协议...