深拷贝与浅拷贝
浅拷贝:只是拷贝了源对象的地址,所以源对象的任何值发生改变了,拷贝对象的值也随之发生改变。
深拷贝:深拷贝拷贝的是源对象的所有值而不是地址,源对象发生改变了,而拷贝对象也不会发生改变。
测试:
package com.ycz.entity;
/**
* @Description:
* @Author: Alex
* @Date 2022-07-03-17:08
* @Version: V1.0
**/
public class User {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public User() {
}
public User(String username, String password) {
this.username = username;
this.password = password;
}
}
浅拷贝测试
package com.ycz.entity;
/**
* @Description:
* @Author: Alex
* @Date 2022-07-03-17:10
* @Version: V1.0
**/
public class Test {
public static void main(String[] args) {
User user01 = new User();
user01.setUsername("abc");
user01.setPassword("123456");
User user02 = user01;
//两个对象的地址查看是否相同
System.out.println(user01);
System.out.println(user02);
//修改了username,看是否有所改变
user01.setUsername("啊实打实大师多");
System.out.println(user01.getUsername());
System.out.println(user02.getUsername());
}
}
结果:
深拷贝测试
修改User,重写clone,实现Cloneable方法
public class User implements Cloneable{
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public User() {
}
public User(String username, String password) {
this.username = username;
this.password = password;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return (User)super.clone();
}
}
测试:
public class Test02 {
public static void main(String[] args) throws CloneNotSupportedException {
User user01 = new User();
user01.setUsername("abc");
user01.setPassword("123456");
User user03 = (User) user01.clone();
System.out.println(user01);
System.out.println(user03);
user01.setUsername("asdasd");
System.out.println(user03.getUsername());
}
}
结果:
会发现地址都是不一样的,修改之后肯定也是不一样的
让我们来复杂一点的
修改user , 此时user重写了clone
package com.ycz.entity;
/**
* @Description:
* @Author: Alex
* @Date 2022-07-03-17:08
* @Version: V1.0
**/
public class User implements Cloneable{
private String username;
private String password;
private Peg peg;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Peg getPeg() {
return peg;
}
public void setPeg(Peg peg) {
this.peg = peg;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public User() {
}
public User(String username, String password , Peg peg) {
this.username = username;
this.password = password;
this.peg = peg;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return (User)super.clone();
}
}
Peg没有重写clone
package com.ycz.entity;
/**
* @Description:
* @Author: Alex
* @Date 2022-07-03-17:36
* @Version: V1.0
**/
public class Peg implements Cloneable{
private String name;
private Integer age;
public Peg(String name, Integer age) {
this.name = name;
this.age = age;
}
public Peg() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
测试
package com.ycz.entity;
/**
* @Description:
* @Author: Alex
* @Date 2022-07-03-17:38
* @Version: V1.0
**/
public class Test03 {
public static void main(String[] args) throws CloneNotSupportedException {
User user = new User();
user.setUsername("abc");
user.setPassword("123456");
Peg peg = new Peg();
peg.setName("小狗");
peg.setAge(18);
user.setPeg(peg);
//试一下深拷贝 , peg为浅拷贝
User user02 = (User) user.clone();
System.out.println(user02);
System.out.println(user);
System.out.println(user02.getPeg());
System.out.println(user.getPeg());
peg.setAge(20);
System.out.println("user-->"+user.getPeg().getAge());
System.out.println("user02-->"+user02.getPeg().getAge());
user.setUsername("abvasda");
System.out.println("user-->"+user.getUsername());
System.out.println("user02-->"+user02.getUsername());
}
}
会发现,user是深拷贝,peg为浅拷贝
继续测试,这时候将peg也加上clone,且重写user里面的clone
package com.ycz.entity;
/**
* @Description:
* @Author: Alex
* @Date 2022-07-03-17:36
* @Version: V1.0
**/
public class Peg implements Cloneable{
private String name;
private Integer age;
public Peg(String name, Integer age) {
this.name = name;
this.age = age;
}
public Peg() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
package com.ycz.entity;
/**
* @Description:
* @Author: Alex
* @Date 2022-07-03-17:08
* @Version: V1.0
**/
public class User implements Cloneable{
private String username;
private String password;
private Peg peg;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Peg getPeg() {
return peg;
}
public void setPeg(Peg peg) {
this.peg = peg;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public User() {
}
public User(String username, String password , Peg peg) {
this.username = username;
this.password = password;
this.peg = peg;
}
@Override
protected Object clone() throws CloneNotSupportedException {
User user = (User) super.clone();
user.setPeg((Peg) user.peg.clone());
return user;
}
}
测试:
package com.ycz.entity;
/**
* @Description:
* @Author: Alex
* @Date 2022-07-03-17:38
* @Version: V1.0
**/
public class Test03 {
public static void main(String[] args) throws CloneNotSupportedException {
User user = new User();
user.setUsername("abc");
user.setPassword("123456");
Peg peg = new Peg();
peg.setName("小狗");
peg.setAge(18);
user.setPeg(peg);
//试一下深拷贝 , peg为浅拷贝
User user02 = (User) user.clone();
System.out.println(user02);
System.out.println(user);
System.out.println(user02.getPeg());
System.out.println(user.getPeg());
peg.setAge(20);
System.out.println("user-->"+user.getPeg().getAge());
System.out.println("user02-->"+user02.getPeg().getAge());
user.setUsername("abvasda");
System.out.println("user-->"+user.getUsername());
System.out.println("user02-->"+user02.getUsername());
}
}
结果:发现都是深拷贝了
再我们测试一下,看都为浅拷贝的结果
package com.ycz.entity;
/**
* @Description:
* @Author: Alex
* @Date 2022-07-03-17:08
* @Version: V1.0
**/
public class User implements Cloneable{
private String username;
private String password;
private Peg peg;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Peg getPeg() {
return peg;
}
public void setPeg(Peg peg) {
this.peg = peg;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public User() {
}
public User(String username, String password , Peg peg) {
this.username = username;
this.password = password;
this.peg = peg;
}
}
package com.ycz.entity;
/**
* @Description:
* @Author: Alex
* @Date 2022-07-03-17:36
* @Version: V1.0
**/
public class Peg implements Cloneable{
private String name;
private Integer age;
public Peg(String name, Integer age) {
this.name = name;
this.age = age;
}
public Peg() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
package com.ycz.entity;
/**
* @Description:
* @Author: Alex
* @Date 2022-07-03-17:38
* @Version: V1.0
**/
public class Test03 {
public static void main(String[] args) throws CloneNotSupportedException {
User user = new User();
user.setUsername("abc");
user.setPassword("123456");
Peg peg = new Peg();
peg.setName("小狗");
peg.setAge(18);
user.setPeg(peg);
//试一下深拷贝 , peg为浅拷贝
User user02 = user;
System.out.println(user02);
System.out.println(user);
System.out.println(user02.getPeg());
System.out.println(user.getPeg());
peg.setAge(20);
System.out.println("user-->"+user.getPeg().getAge());
System.out.println("user02-->"+user02.getPeg().getAge());
user.setUsername("abvasda");
System.out.println("user-->"+user.getUsername());
System.out.println("user02-->"+user02.getUsername());
}
}
结果: