Java克隆技术

通过克隆可以得到一个对象的完整复制。

  • 浅度克隆: 只是实现了对象表层的克隆,对于包含在对象内部的内容并没有克隆
  • 深度克隆: 对包含在对象内部的内容也进行克隆

实现克隆的方法:

1)实现Cloneable接口

2)重写Object类中的protected Object clone() throws CloneNotSupportedException方法并捕获CloneNotSupportedException异常

3)调用super.clone()返回对象

 

浅度克隆举例:

 

class Programmer implements Cloneable{
 private String name;
 private int age;
 private double sal;
 public Programmer(String name,int age,double sal){
  this.name=name;
  this.age=age;
  this.sal=sal;
 }
 
 public Object clone() throws CloneNotSupportedException{
  Programmer pg=(Programmer)super.clone();
  return pg;
 }

 public void changeInformation(String name,int age,double sal){
  this.name=name;
  this.age=age;
  this.sal=sal;
 }
 
 public String toString(){
  return "name:"+name+",age:"+age+",salary:"+sal;
 }
}

public class CloneTest{
 public static void main(String[] args){
 try{
  Programmer pg1=new Programmer("Jackson",23,1000);
  Programmer pg2=(Programmer)pg1.clone();
  pg2.changeInformation("Kevin",25,3000);
  System.out.println("pg1="+pg1);
  System.out.println("pg2="+pg2);
 }catch(CloneNotSupportedException e){
  System.out.println(e.getMessage());
  e.printStackTrace();
 }
 }
}

 

执行结果:

C:\javastudy>java CloneTest
pg1=name:Jackson,age:23,salary:1000.0
pg2=name:Kevin,age:25,salary:3000.0

C:\javastudy>

 

深度克隆举例:

 

对DeepProgrammer进行克隆时,同时对它包含的Manager也进行克隆。

 

class Manager implements Cloneable{
 private String name;
 private int age;
 private double salary;
 public Manager(String name,int age,double salary){
  this.name=name;
  this.age=age;
  this.salary=salary;
 }

 public Object clone() throws CloneNotSupportedException{
  Manager m=(Manager)super.clone();
  return m;
 }
 
 public void changeInformation(String name,int age,double salary){
  this.name=name;
  this.age=age;
  this.salary=salary;
 }

 public String toString(){
  return "M_name:"+name+",M_age:"+age+",M_salary:"+salary;
 }
}

class DeepProgrammer implements Cloneable{
 private String name;
 private int age;
 private double salary;
 private Manager m;
 
 public DeepProgrammer(String name,int age,double salary,Manager m){
  this.name=name;
  this.age=age;
  this.salary=salary;
  this.m=m;
 }

 public Object clone() throws CloneNotSupportedException{
  DeepProgrammer dp=(DeepProgrammer)super.clone();
  dp.setManager((Manager)m.clone());
  return dp;
 }
 
 public void changeInformation(String name,int age,double salary){
  this.name=name;
  this.age=age;
  this.salary=salary;
 }
 
 public void setManager(Manager m){
  this.m=m;
 }
 public Manager getManager(){
  return m;
 }
 public String toString(){
  return "name:"+name+",age:"+age+",salary:"+salary+"  "+m;
 }
}

public class DeepCloneTest{
 public static void main(String[] args){
 try{
  Manager m1=new Manager("Brown",34,10000);
  DeepProgrammer dp1=new DeepProgrammer("Jackson",26,5000,m1);
  DeepProgrammer dp2=(DeepProgrammer)dp1.clone();
  dp2.changeInformation("Black",30,8000);
  dp2.getManager().changeInformation("John",40,20000);
  System.out.println(dp1);
  System.out.println(dp2);
 }catch(CloneNotSupportedException e){
  System.out.println(e.getMessage());
  e.printStackTrace();
 }
 }
}

 

执行结果:

C:\javastudy>java DeepCloneTest
name:Jackson,age:26,salary:5000.0  M_name:Brown,M_age:34,M_salary:10000.0
name:Black,age:30,salary:8000.0  M_name:John,M_age:40,M_salary:20000.0

C:\javastudy>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值