标签:blog http os java 使用 io ar 数据 art
JAVA 对象拷贝
为什么需要有对象拷贝?
对象拷贝相对的自然是引用拷贝。java初学者经常会问,我这个方法要改变一个对象的属性,可以把参数传进去了,为什么没有改变了?
——基本数据类型传值,而对象传引用或引用的拷贝。
而有时候我们要获取到一个当前状态的对象复制品,他们是两个独立对象。不再是引用或者引用拷贝(实质都是指向对象本身)。就是说a是b的拷贝,b发生变化的时候,不要影响a。
对象拷贝有浅拷贝和深度拷贝两种。
1)浅拷贝
浅拷贝是指对象中基本数据类型得到拷贝,而引用数据类型并未拷贝。
提到拷贝自然和clone联系起来了,所有具有clone功能的类都有一个特性,那就是它直接或间接地实现了Cloneable接口。
否则,我们在尝试调用clone()方法时,将会触发CloneNotSupportedException异常。
eg:
public class DOG implements Cloneable
{
public DOG(String name, int age)
{
this .name = name;
this .age = age;
}
public String getName()
{
return this .name;
}
public int getAge()
{
return this .age;
}
public Object clone()
{
try
{
return super .clone();
} catch (CloneNotSupportedException e)
{
return null ;
}
}
public String name;
private int age;
// test
public static void main(String[] args)
{
DOG dog1 = new DOG( " xiaogou " , 2 );
DOG dog2 = (DOG) dog1.clone();
dog1.name = " dagou " ;
System.out.println(dog2.getName());
System.out.println(dog2.getAge());
System.out.println(dog1.getName());
System.out.println(dog1.getAge());
}
} 
运行结果:
xiaogou
2
dagou
2
2)深度拷贝
相对浅拷贝。实现对象中基本数据类型和引用数据类型的拷贝。
请先看下面代码:
class AAA
{
public AAA(String name)
{
this .name = name;
}
public String name;
}
class DOG implements Cloneable
{
public DOG(String name, int age, AAA birthday)
{
this .name = name;
this .age = age;
this .birthday = birthday;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public AAA getBirthday()
{
return birthday;
}
public String getBirth(AAA a)
{
return a.name;
}
public String name;
private int age;
public AAA birthday;
public Object clone()
{
try
{
super .clone();
return super .clone();
} catch (Exception e)
{
return null ;
}
}
}
public class TestClone
{
public static void main(String[] args)
{
AAA Day = new AAA( " test " );
DOG dog1 = new DOG( " xiaogou " , 2 , Day);
DOG dog2 = (DOG) dog1.clone();
// dog2.birthday = (AAA) dog1.birthday.clone();
dog1.birthday.name = " 333 " ;
System.out.println(dog1.getBirth(dog1.birthday));
System.out.println(dog2.getBirth(dog2.birthday));
}
} 
运行结果是:
333
333
而真正要实现拷贝还的加点代码,如下请对比上面和下面代码的异同之处:
class AAA implements Cloneable
{
public AAA(String name)
{
this .name = name;
}
public Object clone()
{
try
{
super .clone();
return super .clone();
} catch (Exception e)
{
return null ;
}
}
public String name;
}
class DOG implements Cloneable
{
public DOG(String name, int age, AAA birthday)
{
this .name = name;
this .age = age;
this .birthday = birthday;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public AAA getBirthday()
{
return birthday;
}
public String getBirth(AAA a)
{
return a.name;
}
public String name;
private int age;
public AAA birthday;
public Object clone()
{
try
{
super .clone();
return super .clone();
} catch (Exception e)
{
return null ;
}
}
}
public class TestClone
{
public static void main(String[] args)
{
AAA Day = new AAA( " test " );
DOG dog1 = new DOG( " xiaogou " , 2 , Day);
DOG dog2 = (DOG) dog1.clone();
dog2.birthday = (AAA) dog1.birthday.clone(); // 特别注意这里
dog1.birthday.name = " 333 " ;
System.out.println(dog1.getBirth(dog1.birthday));
System.out.println(dog2.getBirth(dog2.birthday));
}
} 
运行结果:
333
test
这样基本就达到了我们当初的母的。
但是明显的这种方法还是有许多不足,人们总是希望一个clone就是对象直接克隆。而上面还要对对象中的对象递归使用clone。下面提供一种更高级点的做法:
import java.io. * ;
class AAA implements Serializable
{
public AAA(String name)
{
this .name = name;
}
public String name;
}
class DOG extends SerialCloneable
{
public DOG(String name, int age, AAA birthday)
{
this .name = name;
this .age = age;
this .birthday = birthday;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public AAA getBirthday()
{
return birthday;
}
public String getBirth(AAA a)
{
return a.name;
}
public String name;
private int age;
public AAA birthday;
public Object clone()
{
try
{
super .clone();
return super .clone();
} catch (Exception e)
{
return null ;
}
}
}
public class TestClone
{
public static void main(String[] args)
{
AAA Day = new AAA( " test " );
DOG dog1 = new DOG( " xiaogou " , 2 , Day);
DOG dog2 = (DOG) dog1.clone();
// dog2.birthday = (AAA) dog1.birthday.clone();
dog1.birthday.name = " 333 " ;
System.out.println(dog1.getBirth(dog1.birthday));
System.out.println(dog2.getBirth(dog2.birthday));
}
}
class SerialCloneable implements Cloneable, Serializable
{
public Object clone()
{
try
{
ByteArrayOutputStream bout = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bout);
out.writeObject( this );
out.close();
ByteArrayInputStream bin = new ByteArrayInputStream(bout
.toByteArray());
ObjectInputStream in = new ObjectInputStream(bin);
Object ret = in.readObject();
in.close();
return ret;
} catch (Exception e)
{
return null ;
}
}
} 
输出:
333
test
上面的代码用序列化与反序列化实现了对象拷贝。比较通用。但是得注意的是其中的类得implements Serializable。
3)后记
我们如果利用强大的反射机制+序列化与反序列化,能做出更加灵活的对象拷贝。有兴趣的朋友可以自行去研究。
我在javaeye上看到一篇短文:http://www.javaeye.com/post/367014 主要讲的就是反射在对象拷贝中的应用。
标签:blog http os java 使用 io ar 数据 art
原文地址:http://www.cnblogs.com/wy-NB/p/3938516.html