1对象的克隆(clone)

  单纯的同类的两个对象a0 a00,a0=a00只是栈指向同一个堆,而不是开辟两个新堆,修改其中一个,另一个也会受牵连。

  需要重写Clone()方法,并且实现Cloneable接口。

  浅层克隆:仅仅对对象成员的逐成员克隆

    

package homework.B1.src.com.C12;

/**
* Created by Administrator on 2018/2/27 0027.
* 作业:浅层克隆:
*/
public class A {
public static void main(String[] args) {
A0 a0=new A0();
a0.setI(2);
try {
A0 a00=(A0)a0.clone();
System.out.println(a00.getI());
a0.setI(3);
} catch (CloneNotSupportedException e) {
e.printStackTrace();
} }
}
class A0 implements Cloneable{
private int i;
private String s; public A0(int i, String s) {
this.i = i;
this.s = s;
} public A0() {
} public int getI() {
return i;
} public void setI(int i) {
this.i = i;
} public String getS() {
return s;
} public void setS(String s) {
this.s = s;
} @Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}

  深层克隆:不仅拷贝对象的字段,而且还对对象通过字段关联的其他对象实施拷贝,即对于当前对象关联的其他对象,深克隆要先创建这个其他对象再实施克隆,迭代。

package homework.B1.src.com.C12;

/**
* Created by Administrator on 2018/2/7 0027.
* 深克隆
*/
public class B {
public static void main(String[] args) {
Cat c=new Cat();
Cat c1;
c.setAnimal(new Animal());
c.getAnimal().setName("asd");
try {
c1=(Cat)c.clone();
c.getAnimal().setName("zxc");
System.out.println(c1.getAnimal().getName());
} catch (CloneNotSupportedException e) {
e.printStackTrace();
} }
} class Cat implements Cloneable{
private Animal animal;
private String name;
private int age; @Override
protected Object clone() throws CloneNotSupportedException {
Animal animal1=(Animal)this.animal.clone();
Cat cat1=(Cat)super.clone();
cat1.setAnimal(animal1);
return cat1;
} public Cat() {
} public Cat(Animal animal, String name, int age) {
this.animal = animal;
this.name = name;
this.age = age;
} public Animal getAnimal() {
return animal;
} public void setAnimal(Animal animal) {
this.animal = animal;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
}
}
class Animal implements Cloneable{
private String name; public Animal(String name) {
this.name = name;
} public Animal() {
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} @Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}

应用:有些对象创建复杂,因此使用clone来进行对象创建,实现了原型模式;

最新文章

  1. asp.net中用cookie记住密码上次不用登陆
  2. 关于iphone 6 ios8网站背景图片错乱的问题解决办法
  3. 【bzoj1018】 SHOI2008—堵塞的交通traffic
  4. 3ds max不显示网格,转换为可编辑面片
  5. Android课程---环境配置很重要
  6. INFORMATICA 的部署实施之 BACKUP&RESTORE
  7. 源码安装zabbix
  8. 关于cookie的清除
  9. Axis,axis2,Xfire以及cxf对比
  10. java 关于extends 和implement的区别
  11. Hive SQL运行状态监控(HiveSQLMonitor)
  12. currentStyle、getComputedStyle
  13. C#学习日志 day 5 plus------ interface 数组及stringBuilder相关
  14. Spring jdbctemplate学习笔记
  15. ios常见问题 经验之谈
  16. Linux下Scala(2.12.1)安装
  17. jQuery和js获取select元素的选中项value?
  18. iOS开发--XMPPFramework--好友列表(五)
  19. 2017-2018-1 20155306 《信息安全系统设计基础》Mybash的实现
  20. JAVA进阶20

热门文章

  1. NOIP2018提高组金牌训练营——字符串专题
  2. React基础知识点全解
  3. 【codeforces 235B】Let's Play Osu!
  4. mybatis插入操作时,返回自增主键id
  5. ASP.NET-未解决的问题
  6. Oracle 学习笔记 14 -- 集合操作和高级子查询
  7. 多项福利回馈会员,且看Hao123怎样玩转“霸权主义”
  8. 从头认识java-15.3 使用HashSet须要注意的地方
  9. Codeforce 163 A. Substring and Subsequence DP
  10. (转载) Android 带清除功能的输入框控件ClearEditText,仿IOS的输入框