频道直达 - 专题 - 新闻 - 技巧 - 组网 - 开发 - 安全 - web编程 - 图像 - 操作系统 - 数据库 - 教育 - 旅游 - 健康 - 时尚 - 驱动 - 软件 - 游戏 - 多媒体 - ERP - 讨论组

JAVA 对象拷贝

来源:BlogJava 作者:274°C 出处:巧巧读书 2008-05-16 进入讨论组
  • 关 键 词:
  • java
下一页 1 2 

为什么需要有对象拷贝?

对象拷贝相对的自然是引用拷贝。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)深度拷贝

相对浅拷贝。实现对象中基本数据类型和引用数据类型的拷贝。

请先看下面代码:

import java.io.*;
import java.util.*;


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
而真正要实现拷贝还的加点代码,如下请对比上面和下面代码的异同之处:

import java.io.*;
import java.util.*;


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
这样基本就达到了我们当初的母的。

更多文章 更多内容请看Java环境安装配置Java编程开发手册Java对象专题,或进入讨论组讨论。
下一页 1 2 
收藏此文】【 】【打印】【关闭
相关图文阅读
频道图文推荐
健 康 咨 询
时 尚 咨 询
巧巧读书宗旨
相关专题
讨论组问题推荐
站内各频道最新更新文档
站内最新制作专题
热门关键字导读
Photoshop教 程照片处理 照片制作 PS快捷键 抠图
计 算 机 故 障XP系统修复
艺 术 与 设 计设计 流媒体 设计欣赏 边框
计 算 机 安 全ARP
站内频道文章精选
巧巧电脑频道编辑信箱  告诉我们您想看的专题或文章