一 对象拷贝
1.1 浅拷贝
1.浅拷贝:
对于基本数据复制的是值;
引用类型复制的是内存引用地址,源对象只是复制了一份引用地址给拷贝对象,拷贝对象和源对象在堆内存中指向同一个对象;
1.2 深拷贝
2.深拷贝:拷贝对象在堆内存中 new 出一个源对象,在堆内存指向两个不同的对象,拷贝对象和源对象是两个独立的对象,互相隔离。
1.3 操作步骤
1.实现Cloneable接口,重写clone方法
二 案例操作
2.1 浅拷贝
1.boss代码
public class Boss implements Cloneable{
private String bossName;
private String title;
public Boss() {
}
public Boss(String bossName, String title) {
this.bossName = bossName;
this.title = title;
}
public String getBossName() {
return bossName;
}
public void setBossName(String bossName) {
this.bossName = bossName;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
// @Override
// protected Object clone() throws CloneNotSupportedException
// {
// return super.clone();
// }
}
2.Emp代码
1截图
2.代码
public class Emp implements Cloneable{
private String empName;
private Integer age;
private Boss boss;
public Emp() {
}
public Emp(String empName, Integer age, Boss boss) {
this.empName = empName;
this.age = age;
this.boss = boss;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Boss getBoss() {
return boss;
}
public void setBoss(Boss boss) {
this.boss = boss;
}
public Emp(String empName, Integer age, String bossName, String title)
{
this.empName = empName;
this.age = age;
this.boss = new Boss(bossName,title);
}
/**
* @author admin
* @description 浅拷贝
* @date 2024/7/14 9:14
* @param []
* @return java.lang.Object
*/
protected Object clone() throws CloneNotSupportedException
{
return super.clone();
}
// /**
// * @author admin
// * @description 深拷贝
// * @date 2024/7/14 9:14
// * @param []
// * @return java.lang.Object
// */
// @Override
// protected Object clone() throws CloneNotSupportedException
// {
// return new Emp(empName,age,boss.getBossName(),boss.getTitle());
// }
}
3.测试验证
2.2 深拷贝
1.boss代码
public class Boss implements Cloneable{
private String bossName;
private String title;
public Boss() {
}
public Boss(String bossName, String title) {
this.bossName = bossName;
this.title = title;
}
public String getBossName() {
return bossName;
}
public void setBossName(String bossName) {
this.bossName = bossName;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
// @Override
// protected Object clone() throws CloneNotSupportedException
// {
// return super.clone();
// }
}
2.emp代码
关键点:
代码:
public class Emp implements Cloneable{
private String empName;
private Integer age;
private Boss boss;
public Emp() {
}
public Emp(String empName, Integer age, Boss boss) {
this.empName = empName;
this.age = age;
this.boss = boss;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Boss getBoss() {
return boss;
}
public void setBoss(Boss boss) {
this.boss = boss;
}
public Emp(String empName, Integer age, String bossName, String title)
{
this.empName = empName;
this.age = age;
this.boss = new Boss(bossName,title);
}
/**
* @author admin
* @description 浅拷贝
* @date 2024/7/14 9:14
* @param []
* @return java.lang.Object
*/
// protected Object clone() throws CloneNotSupportedException
// {
// return super.clone();
// }
/**
* @author admin
* @description 深拷贝
* @date 2024/7/14 9:14
* @param []
* @return java.lang.Object
*/
@Override
protected Object clone() throws CloneNotSupportedException
{
return new Emp(empName,age,boss.getBossName(),boss.getTitle());
}
}
3.测试查看