ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

JAVA中Clone方法的使用

2022-09-14 20:34:29  阅读:202  来源: 互联网

标签:JAVA name Course Clone id course 方法 public String


参考:https://www.cnblogs.com/Kevin-ZhangCG/p/9088619.html


影子克隆也就是浅克隆

浅克隆

package com.pillar.test.clone.demo02.shallowcopy;

/**
 * @author Pillar
 * @version 1.0
 * @date 2022/9/14 19:36
 */
public class Teacher implements Cloneable {
    private String name;
    private Integer age;
    private Course course;

    public Teacher() {
    }

    public Teacher(String name, Integer age, Course course) {
        this.name = name;
        this.age = age;
        this.course = course;
    }

    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;
    }
    public Course getCourse() {
        return course;
    }
    public void setCourse(Course course) {
        this.course = course;
    }

    @Override
    protected Teacher clone() throws CloneNotSupportedException {
        return (Teacher) super.clone();
    }
    @Override
    public String toString() {
        return "Teacher [name=" + name + ", age=" + age + ", course=" + course + "]";
    }
}
package com.pillar.test.clone.demo02.shallowcopy;

public class Course {
    
    private String name;
    private Integer id;

    public Course() {
    }

    public Course(String name, Integer id) {
        this.name = name;
        this.id = id;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    @Override
    public String toString() {
        return "Course [name=" + name + ", id=" + id + "]";
    }
    

}
package com.pillar.test.clone.demo02.shallowcopy;

/**
 * @author Pillar
 * @version 1.0
 * @date 2022/9/14 19:16
 */
/*
影子克隆
    调用clone方法产生的效果是:现在内存中开辟一块和原始对象一样的空间,然后拷贝原始对象中的内容
    但对于非基本类型,它们保存的仅仅是对象的引用
为了解决影子克隆所产生的问题,我们就需要使用深度克隆方案。
 */
public class TestClone {
    public static void main(String[] args) {
        Teacher teacher1 = new Teacher("zhangsan",18,new Course("语文",1));
        System.out.println("teacher1 ->" + teacher1);
        try {
            Teacher teacher2 = teacher1.clone();
            System.out.println("course是否相同: "+teacher1.getCourse().equals(teacher2.getCourse()));
            System.out.println("teacher2 ->" + teacher2);
            System.out.println(teacher1.equals(teacher2));
            System.out.println("Alter teacher2......");
            teacher2.setName("Lisi");
            teacher2.setAge(20);
            teacher2.getCourse().setName("English");
            teacher2.getCourse().setId(88);
            System.out.println("teacher1=>"+teacher1);
            System.out.println("teacher2=>"+teacher2);
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
    }
}

深拷贝

package com.pillar.test.clone.demo02.deepcopy;


/**
 * @author Pillar
 * @version 1.0
 * @date 2022/9/14 19:36
 */
public class Teacher implements Cloneable {
    private String name;
    private Integer age;
    private Course course;

    public Teacher() {
    }

    public Teacher(String name, Integer age, Course course) {
        this.name = name;
        this.age = age;
        this.course = course;
    }

    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;
    }
    public Course getCourse() {
        return course;
    }
    public void setCourse(Course course) {
        this.course = course;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        Teacher teacher = (Teacher)super.clone();
        teacher.course = course.clone();
        return teacher;
    }

    @Override
    public String toString() {
        return "Teacher [name=" + name + ", age=" + age + ", course=" + course + "]";
    }
}
package com.pillar.test.clone.demo02.deepcopy;

public class Course implements Cloneable{


    private String name;
    private Integer id;

    public Course() {
    }

    public Course(String name, Integer id) {
        this.name = name;
        this.id = id;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    @Override
    protected Course clone() throws CloneNotSupportedException {
        return (Course) super.clone();
    }
    @Override
    public String toString() {
        return "Course [name=" + name + ", id=" + id + "]";
    }
}
package com.pillar.test.clone.demo02.deepcopy;

/**
 * 测试clone方法
 * @author Kevin
 *
 */

public class TestClone {

    public static void main(String[] args){
        Teacher t1 = new Teacher();
        t1.setName("Kevin");
        t1.setAge(22);
        Course c1 = new Course();
        c1.setName("Math");
        c1.setId(66);
        t1.setCourse(c1);
        System.out.println("teacher1"+t1);

        try{
            Teacher t2 = (Teacher) t1.clone();
            System.out.println("Clone teacher2 from teacher1...");
            System.out.println("teacher2"+t2);
            System.out.println("Alter teacher2...");
            t2.setName("Ryan");
            t2.setAge(18);
            //修改courese属性
            t2.getCourse().setName("English");
            t2.getCourse().setId(88);
            System.out.println("teacher1"+t1);
            System.out.println("teacher2"+t2);
        }catch(CloneNotSupportedException e){
            e.printStackTrace();
        }
    }
}

浅拷贝拷贝了对象本身,但对于非基本类型,指向同一引用地址;
深拷贝拷贝了对象本身,也拷贝了引用对象

标签:JAVA,name,Course,Clone,id,course,方法,public,String
来源: https://www.cnblogs.com/do-it-520/p/16694368.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有