
拷贝
拷贝就拷贝 分什么浅深呀
浅拷贝: 我就帮到你复制基本类型跟复制对象的地址 我就只能到这了
深拷贝:那要是复制后对象的属性中包含的对象属性被人更改了 变了 就都变了 怎么办
浅拷贝:那我没办法 看你的了
深拷贝:复制都不把属性中对象属性都复制一遍 真是人如其名呀
浅拷贝
public class Person implements Cloneable
{
private int age;
public Person(int age)
{
super();
this.age = age;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
@Override
protected Object clone()
{
try
{
return super.clone();
} catch (CloneNotSupportedException e)
{
e.printStackTrace();
}
return null;
}
}
深拷贝
public class Prototype implements Cloneable
{
private String name;
private Person person;
public Prototype(String name)
{
super();
this.name = name;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public Person getPerson()
{
return person;
}
public void setPerson(Person person)
{
this.person = person;
}
@Override
protected Object clone()
{
Prototype p=null;
try
{
p=(Prototype)super.clone();
p.person=(Person)person.clone();
} catch (CloneNotSupportedException e)
{
e.printStackTrace();
}
return p;
}
}
网友评论