全部博文(2065)
分类: Java
2010-06-13 17:18:40
Java中对象复制
时间:
情况一、简单的引用拷贝
package org.test;
public class
Clone {
public static void main(String[] args) {
People p1 = new
People();
p1.setNameString("p1");
System.out.println(p1.getNameString());
People p2 = p1;
p2.setNameString("this is p2");
System.out.println(p2.getNameString());
System.out.println(p1.getNameString());
}
}
class People {
private String
nameString;
public String
getNameString() {
return
nameString;
}
public void setNameString(String nameString)
{
this.nameString
= nameString;
}
}
输出:
p1
this is p2
this is p2
obj2 = obj1; 实际上没有拷贝对象而仅仅是拷贝了一个对象引用,换换言之,在你执行这个操作后仍然只有一个对象,但是多出了一个对该对象的引用。
我的理解:
实际的内存区域就是一块。Obj1与obj2都是些引用。它们的操作都是针对同一块内存区域的。
现在我想要实现对象的拷贝的话应当如何处理呢?
package org.test;
public class Clone {
public static
void main(String[] args) {
People p1 = new People();
p1.setNameString("p1");
System.out.println(p1.getNameString());
People p2 =
(People)p1.clone();
p2.setNameString("this
is p2");
System.out.println(p2.getNameString());
System.out.println(p1.getNameString());
}
}
class People implements Cloneable {
private
String nameString;
public
String getNameString() {
return
nameString;
}
public void
setNameString(String nameString) {
this.nameString
= nameString;
}
public
Object clone() {
try
{
return
super.clone();
} catch
(Exception e) {
try
{
throw
new Exception(e.toString());
} catch
(Exception e1) {
//
TODO Auto-generated catch block
e1.printStackTrace();
}
}
return
null;
}
}
输出:
p1
this is p2
p1
看到没有。现在是分开了是吧。单独的处理方式了!
再进一步:我把代码修改成
package org.test;
import java.util.HashMap;
public class Clone {
public
static void main(String[] args) {
People
p1 = new People();
People
p2 = (People)p1.clone();
p1.map.remove("key1");
System.out.println(p2.map.get("key1"));
}
}
class People implements Cloneable {
private
String nameString;
public
HashMap map;
public
People() {
map
= new HashMap();
map.put("key1",
"value1");
map.put("key2",
"value2");
}
public
String getNameString() {
return
nameString;
}
public
void setNameString(String nameString) {
this.nameString
= nameString;
}
public
Object clone() {
try
{
return
super.clone();
}
catch (Exception e) {
try
{
throw
new Exception(e.toString());
}
catch (Exception e1) {
//
TODO Auto-generated catch block
e1.printStackTrace();
}
}
return
null;
}
}
我们发现p1处理完了之后P2也没有了。输出 null值!说明还是不独立的!
解决办法 :
package org.test;
import java.util.HashMap;
public class Clone {
public
static void main(String[] args) {
People
p1 = new People();
People
p2 = (People)p1.clone();
p1.map.remove("key1");
System.out.println(p2.map.get("key1"));
}
}
class People implements Cloneable {
private
String nameString;
public
HashMap map;
public
People() {
map
= new HashMap();
map.put("key1",
"value1");
map.put("key2",
"value2");
}
public
String getNameString() {
return
nameString;
}
public
void setNameString(String nameString) {
this.nameString
= nameString;
}
public
Object clone() {
try
{
People aobj =
(People)super.clone();
aobj.map = (HashMap)map.clone();//这个也要复制一份
return aobj;
}
catch (Exception e) {
try
{
throw
new Exception(e.toString());
}
catch (Exception e1) {
//
TODO Auto-generated catch block
e1.printStackTrace();
}
}
return null;
}
}
相应的其他的复制像数组复制
int vec1[]
= new int[]{1, 2, 3};
int vec2[]
= (int[])vec1.clone();
System.out.println(vec2[0] + " " +
vec2[1] +" " + vec2[2]);