什么是浅克隆和深克隆?

1、在我们的开发过程中经常遇到要复制一个对象比如: 

  @Test
    public void test1(){
        People p1 = new People("1","zhang3",12);
        People p2 = p1 ;
        System.out.println(p1==p2);// 输出  true
        p1.setUsername("li4");
        System.out.println(p2.getUsername()); // 输出 li4

    }

2、浅克隆 :对象进行自我复制 ,返回一个新对象。新对象的所有变量都与自己的变量值相同,而新对象所有对其他对象的引用仍然和原对象的引用值一样。

实现浅克隆的三个步骤:

  • 实现 Cloneable 接口。
  • 重写Object类中的clone()方法 ,并声明为public 。
  • 在重写的clone()方法中调用 super.clone()  。

  @Override
    public Object clone() throws CloneNotSupportedException {
        People p = null;
        try {
            p = (People) super.clone();
        }catch(CloneNotSupportedException e){
            e.printStackTrace();
        }
         return p ;
    } 
  @Test
    public void test2()throws CloneNotSupportedException{
        People p1 = new People("1","zhang3",12);
        People p2 = (People) p1.clone();
        System.out.println(p1==p2);// 输出 false
        p1.setUsername("li4");
        System.out.println(p2.getUsername()); // 输出 zhang3
    }

 初步解决,发现问题

package com.wcly.activemqTest;

public class Student {

    private String classNo;
    private String teacherName ;

    public String getTeacherName() {
        return teacherName;
    }

    public void setTeacherName(String teacherName) {
        this.teacherName = teacherName;
    }

    public String getClassNo() {
        return classNo;
    }

    public void setClassNo(String classNo) {
        this.classNo = classNo;
    }

    public Student(String classNo, String teacherName) {
        this.classNo = classNo;
        this.teacherName = teacherName;
    }
}
package com.wcly.activemqTest;

public class People implements Cloneable{

    private String id;
    private String username;
    private int age;
    private Student stu ;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Student getStu() {
        return stu;
    }

    public void setStu(Student stu) {
        this.stu = stu;
    }

    public People(String id, String username, int age,Student stu) {
        this.id = id;
        this.username = username;
        this.age = age;
        this.stu = stu ;
    }



    @Override
    public boolean equals(Object obj) {

        if (this == obj) {
            return true;
        }
        if(obj instanceof People) {
            People b = (People) obj ;
            if (this.getId() == b.getId()
                    && this.getUsername().equals(b.getUsername())
                    && this.getAge() == b.getAge()) {
                return true;
            }
        }
        return false;
    }

    @Override
    public Object clone() throws CloneNotSupportedException {
        People p = null;
        try {
            p = (People) super.clone();
        }catch(CloneNotSupportedException e){
            e.printStackTrace();
        }
         return p ;
    }
}

 表明 当我们在进行浅克隆的时候 如果这个对象 存在对其他对象的引用,那么克隆后的引用值,和原对象的引用是一样

 @Test
    public void test3()throws CloneNotSupportedException{
        People p1 = new People("1","zhang3",12,new Student("1","teacher"));
        People p2 = (People) p1.clone();
        System.out.println(p1==p2);// 输出 false
        p1.setUsername("li4");
        System.out.println(p2.getUsername()); // 输出 zhang3
        p1.getStu().setClassNo("2");
        System.out.println(p2.getStu().getClassNo()); // 输出 2
        System.out.println(p1.getStu()==p2.getStu());// 输出 true
    }

 3、深克隆: 顾名思义 跟浅克隆相比的区别是 克隆后的对象的对其他对象引用与原对象不同。

实现方式:

  • 引用对象也实现 Cloneable 接口 重载 clone()方法 ,并用public修饰
  • 在People对象的 clone 方法中增加 引用对象的 克隆方法
package com.wcly.activemqTest;

public class Student implements Cloneable {

    private String classNo;
    private String teacherName ;

    public String getTeacherName() {
        return teacherName;
    }

    public void setTeacherName(String teacherName) {
        this.teacherName = teacherName;
    }

    public String getClassNo() {
        return classNo;
    }

    public void setClassNo(String classNo) {
        this.classNo = classNo;
    }

    public Student(String classNo, String teacherName) {
        this.classNo = classNo;
        this.teacherName = teacherName;
    }

    @Override
    public Object clone() throws CloneNotSupportedException {
        Student stu = null;
        try {
            stu = (Student) super.clone();
        }catch(CloneNotSupportedException e){
            e.printStackTrace();
        }
        return stu ;
    }
}
 @Override
    public Object clone() throws CloneNotSupportedException {
        People p = null;
        try {
            p = (People) super.clone();
            p.stu = (Student)stu.clone();
        }catch(CloneNotSupportedException e){
            e.printStackTrace();
        }
         return p ;
    }

 

 @Test
    public void test3()throws CloneNotSupportedException{
        People p1 = new People("1","zhang3",12,new Student("1","teacher"));
        People p2 = (People) p1.clone();
        System.out.println(p1==p2);// 输出 false
        p1.setUsername("li4");
        System.out.println(p2.getUsername()); // 输出 zhang3
        p1.getStu().setClassNo("2");
        System.out.println(p2.getStu().getClassNo()); // 输出 1
        System.out.println(p1.getStu()==p2.getStu());// 输出 false
    }

成功解决。 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

博士通

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值