JAVA中Clone方法的使用
作者:互联网
参考: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