62,621
社区成员
发帖
与我相关
我的任务
分享
public class Ca {
private String id;
private String name;
public Ca (String id, String name) {
this.id = id;
this.name = name;
}
public class Cb {
private String id;
private Ca ca;
public Cb(String id, Ca ca) {
this.id = id;
this.ca = ca;
}
}
Ca ca = new Ca("id1", "name1");
Cb cb = new Cb("id2", ca);
Ca c1 = cb.getCa();
Ca c2 = cb.getCa();
c1.setId("idd");
System.out.println(cb.getCa().getId());
System.out.println(c2.getId());
c1.setId("idd"); 的修改,会影响到cb 和 c2
public class Ca implements Cloneable{
private String id;
private String name;
public Ca (String id, String name) {
this.id = id;
this.name = name;
}
public Object clone() throws CloneNotSupportedException {
Ca cloneObj = (Ca) super.clone();
return cloneObj;
}
然后
public static void main(String[] args) throws CloneNotSupportedException {
Ca ca = new Ca("id1", "name1");
Cb cb = new Cb("id2", ca);
Ca c = (Ca) cb.getCa().clone();
Ca c1 = cb.getCa();
c1.setId("idd");
System.out.println(cb.getCa().getId());
System.out.println(c.getId());
}
此时,c 为拷贝出去的Ca对象,c1 的修改不影响c 了。跟着楼主的问题,了解了下clone 
//分别赋值给 tzsj1 tzsj2 变量
// tzsj1 和 tzsj2 指向相同的内存空间
byte[] tzsj1 = template.getTzsj();
byte[] tzsj2 = template.getTzsj();
if("1".equals(sfzh)){//生成11 22 txt文件
// bytesToFile 不对第二个参数做修改,故tzsj1和tzsj2 依然是之前的样子,且相同
// 同时 11.txt 和 22.txt 也相同
this.bytesToFile("/gfserver/lib/11.txt", tzsj1);//通过tzsj1
this.bytesToFile("/gfserver/lib/22.txt", tzsj2);//通过tzsj2
}
// byteFeature 这个方法,应该会对传参做修改
// 故不经过此方法,四个文件相同,经过此方法,文件两两相同,但不四个相同
scoreFloat = com.byteFeature(imgBytes, tzsj1);//注意我传的是tzsj1
if("1".equals(sfzh)){//33 44 txt文件
// tzsj1 和 tzsj2 相同,故33.txt 和 44.txt 相同
this.bytesToFile("/gfserver/lib/33.txt", tzsj1);//通过tzsj1
this.bytesToFile("/gfserver/lib/44.txt", tzsj2);//通过tzsj2
}
tzsj1 和 tzsj2 始终相同,两次结果不同的影响因素就是byteFeature 方法了