一:直接赋值
public class Dog implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Dog [name=" + name + "]";
}
}
public class Test1 {
public static void main(String[] args) {
Dog dog0 = new Dog();
dog0.setName("小黑");
//直接赋值
Dog dog1 = dog0;
//查看结果
System.out.println(dog0.toString());
System.out.println(dog1.toString());
//改变dog0的name值
dog0.setName("小白");
//查看结果
System.out.println(dog0.toString());
System.out.println(dog1.toString());
}
}
改变其中任意一个对象,另一个也会跟着改变
二:浅拷贝(注意里面有个dog对象)
实现Cloneable接口,并重载clone方法
public class CopyObj implements Cloneable{
private String name;
private Dog dog;
public Object clone(){
try {
return super.clone();
} catch (CloneNotSupportedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
@Override
public String toString() {
return "CopyObj [dog=" + dog + ", name=" + name + "]";
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class Test2 {
public static void main(String[] args) {
//设置初始属性值
Dog dog = new Dog();
dog.setName("小黑");
CopyObj a = new CopyObj();
a.setDog(dog);
a.setName("小明");
//调用clone方法
CopyObj b = (CopyObj) a.clone();
//查看a和b的属性值
System.out.println("a:"+a.toString());
System.out.println("b:"+b.toString());
//改变a中的基本数据类型的属性值
a.setName("小强");
//再查看a和b的属性值,你会发现a的改变了,而b的没有改变
System.out.println("a:"+a.toString());
System.out.println("b:"+b.toString());
//继续...修改dog的name
dog.setName("小白");
//再来查看属性值
System.out.println("a:"+a.toString());
System.out.println("b:"+b.toString());
//a和b里面的dog的name一起改变了,这就是浅拷贝的不足之处:
//如果对象里面的属性值都是基本数据类型,这两个对象任意一个改变都不会影响对方的
//但是如果对象里面某个属性是引用类型,那么就会复制这个属性(dog)的引用,
//在改变属性(dog)里面的属性时,相应的对象(a和b)里面的dog也会跟着改变
}
}
结果:三:深克隆
实现Serializable接口
public class CloneObj implements Serializable{
private static final long serialVersionUID = 1L;
private String name;
private Dog dog;
@Override
public String toString() {
return "CloneObj [dog=" + dog + ", name=" + name + "]";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
}
public class CloneUtil {
/**
* 深克隆
* @param <T>
* @param obj
* @return
* @throws IOException
* @throws ClassNotFoundException
*/
@SuppressWarnings("unchecked")
public static <T extends Serializable>T clone(T obj)
throws IOException, ClassNotFoundException{
//将对象写入到流里面
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(obj);
//从流里面读取对象
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bais);
return (T) ois.readObject();
}
}
public class Test3 {
public static void main(String[] args)
throws IOException, ClassNotFoundException {
//设置初始值
Dog dog = new Dog();
dog.setName("小黑");
CloneObj a = new CloneObj();
a.setDog(dog);
a.setName("小明");
//通过序列化反序列化深克隆
CloneObj b = CloneUtil.clone(a);
//查看属性值
System.out.println("a:"+a.toString());
System.out.println("b:"+b.toString()+"\n");
//修改dog的name
dog.setName("小白");
System.out.println("a:"+a.toString());
System.out.println("b:"+b.toString()+"\n");
//最后a里面的dog的name改变了
//而b里面的dog的name没有改变
//这就是深克隆
}
}
结果: