克隆有“浅层克隆”、深层克隆,什么情况下需要深层克隆,个人有点想法,且看代码:
public class DeepClone implements Cloneable {
private int type;
private final String special;
private Flower aflower;
private int size = 0;
private Flower[] elements;//DeepClone自己管理这部分内存
public DeepClone(int type, String special, Flower flower) {
this.type = type;
this.special = special;
this.aflower = flower;
this.elements = new Flower[10];
}
public void push(Flower e) {
ensureCapacity();
elements[size++] = e;
}
public Object pop() {
if (size == 0)
throw new EmptyStackException();
Object result = elements[--size];
elements[size] = null; // Eliminate obsolete reference
return result;
}
public boolean isEmpty() {
return size == 0;
}
// Ensure space for at least one more element.
private void ensureCapacity() {
if (elements.length == size)
elements = Arrays.copyOf(elements, 2 * size + 1);
}
@Override
public Object clone() {
try {
DeepClone result = (DeepClone) super.clone();
result.elements=this.elements.clone();//(2)
return result;
} catch (CloneNotSupportedException e) {
throw new AssertionError();
}
}
public static void main(String[] arr) {
Flower flower = new Flower();
flower.setName("Red flower");
DeepClone source = new DeepClone(10, "GG", flower);
Flower yellow = new Flower();
yellow.setName("Yellow");
Flower[] arrs=new Flower[]{yellow};
for (Flower arg : arrs)
source.push(arg);
System.out.println("----------源对象花朵名称---------" + source.aflower.getName());
DeepClone copy = (DeepClone) source.clone();
System.out.println("----------克隆的对象花朵名称---------" + copy.aflower.getName());
System.out.println("------source==copy------------"+(source==copy));
source.type=0;
System.out.println("----------源对象type值变化---------" + source.type);
System.out.println("----------克隆的对象type值未发生变化,仍然为10---------" + copy.type);
source.aflower=null;
System.out.println("----------源对象花朵名称变化---------" + (source.aflower==null?"null":source.aflower.getName()));
System.out.println("----------克隆的对象花朵引用未发生变化---------" + (copy.aflower==null?"null":copy.aflower.getName()));
//elements,DeepClone自己管理的内存部分
//如果没有(2)的深层克隆,copy的elements不是克隆一份,而是原有elements的引用
Object sourcePop=null;
while (!source.isEmpty()){
sourcePop=source.pop();
System.out.println("------source elements------------"+((Flower)sourcePop).getName());
}
Object copyPop=null;
while (!copy.isEmpty()){
copyPop=copy.pop();
System.out.println("------copy elements------------"+((Flower)copyPop).getName());
}
System.out.println("------sourcePop==copyPop------------"+(sourcePop==copyPop));
}
}
通过对DeepClone的测试分析,目前有如下结论:
(1)对象域需要“深层克隆”。
(2)对象数组域需要“深层克隆”。
(3)与stack的Object[] elements类似, Flower[] elements域由于是被克隆类DeepClone自己管理的数组;
在DeepColen被克隆时必须“深层克隆”,result.elements=this.elements.clone()才能确保克隆对象有另外一份elements数组(可能是内存地址的指针)。
分享到:
相关推荐
Java中的克隆(Clone)机制,很少涉及的内容。
darts-clone-java 用Java编写的Double-ARray Trie System克隆。 该库基于称为“快速高效”库的 。入门设置要使用Maven添加依赖项,请使用以下命令: < dependency> < groupId>...
airbnb-clone, 使用 Rails Trieu的教程克隆 Airbnb 使用 Rails 克隆 Airbnb Trieu教程访问 https://code4startup.com/projects/airalien-clone-airbnb-with-ruby-on-rails-bootstrap-jque
语音克隆模型
克隆管理克隆管理克隆管理克隆管理克隆管理克隆管理克隆管理克隆管理克隆管理克隆管理克隆管理克隆管理克隆管理克隆管理克隆管理克隆管理克隆管理克隆管理克隆管理克隆管理克隆管理克隆管理克隆管理克隆管理克隆管理...
原创Matlab克隆优化函数-clone.m 这是一个克隆优化函数的程序,希望对大家有用
gitlab 克隆 用于将整个 gitlab 结构轻松克隆到本地机器的工具。 自由软件:MIT 许可证 要求 要求 Python> = 3.6 安装 您可以通过安装“ gitlab-clone”: $ pip install gitlab-clone 用法 >>> gitlab - clone: ...
react-native-instagram-clone:instagram与世博会克隆
git-clone-repos 通过shell命令克隆多个git仓库 安装 npm i git-clone-repos 用法 const clone = require ( 'git-clone-repos' ) ; ( async ( ) => { await clone ( [ 'git@github....
twitter-clone, 一种基于 Ruby on Rails的Twitter 开源克隆 twitter克隆使用 Ruby on Rails http://twitter-clone-rails.herokuapp.com/的Twitter的开源克隆。正在启动git clone https://github.co
java中clone的详细用法,分浅拷贝和深拷贝,并分别有详细的实例介绍。从原理分析。
中文语音克隆内含数据集和预训练模型:voice clone
图像清晰化系列-图片克隆放大器clone v2.0免费版
详细的描述了Java中 clone方法使用
android-java-organizze-clone
用 Java 语言编写的 初学者可学习的clone()
Google-translate-clone:Google翻译克隆
clone的用法 希望有帮助,仅供参考 通过例子的分析,可以对克隆的方法有所深入了解
Java SOAP服务 我的演示使用SOAP服务。 先决条件 8+ 3+ 怎么跑 克隆 git clone https://github.com/humbertodias/java-soap-demo 内 cd java-soap-demo 跑 mvn package mvn embedded-glassfish:run -Dhttp.port=...