1.克隆
克隆是指创建对象的一个副本,使副本具有与原始对象相同的属性和状态。
而克隆又分深克隆和浅克隆。
2.浅克隆
浅克隆:只会把原对象中类型为值的属性复制一份,然后引用数据类型就把它们在内存中的地址复制过去
比如一个对象有
public int age;public String name;public int[] arr;public Address address;
这时候浅拷贝出来了一个对象,改变这个克隆对象的age和name之后,当通过set方法改变了克隆对象的age和name,克隆对象的age和name和原对象就不同
如果试图改变arr或者address的话,那么就原对象也会跟着改变,因为引用对象拷贝的只是地址
下面看代码示例
class Address{public String city;public String getCity() {return city;}public void setCity(String city) {this.city = city;}
}
class Human implements Cloneable{public int age;public String name;public Address address;public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Address getAddress() {return address;}public void setAddress(Address address) {this.address = address;}public Object clone() throws CloneNotSupportedException {return super.clone();}
}
public class Test15 {public static void main(String[] args) throws CloneNotSupportedException {Address address=new Address();address.setCity("北京");Human human=new Human();human.setAge(10);human.setName("小白");human.setAddress(address);System.out.println("克隆前:");System.out.println(human.getName());System.out.println(human.getAge());System.out.println(human.getAddress().getCity());System.out.println("===============================================");Human humanClone= (Human) human.clone();humanClone.setName("大白");humanClone.setAge(20);address.setCity("上海");humanClone.setAddress(address);System.out.println("克隆后:");System.out.println(humanClone.getName());System.out.println(humanClone.getAge());System.out.println(humanClone.getAddress().getCity());//下面这行代码的结果你会发现原对象的city也变成上海了System.out.println(human.getAddress().getCity());System.out.println("===============================================");System.out.println("克隆后的引用类型对比");System.out.println("原对象:"+human.getAddress().getCity());System.out.println("拷贝对象:"+humanClone.getAddress().getCity());System.out.println("===============================================");System.out.println("克隆后的基本数据类型对比");System.out.println("原对象:"+human.getName());System.out.println("原对象:"+human.getAge());System.out.println("拷贝对象:"+humanClone.getName());System.out.println("拷贝对象:"+humanClone.getAge());}
}
输出结果是
3.深克隆
深克隆:将原对象的所有类型,都拷贝一份。
深克隆的话,如果你修改arr或者address的话,原对象不会跟着改变了
下面看代码
class Address implements Cloneable{public String city;public String getCity() {return city;}public void setCity(String city) {this.city = city;}public Object clone() throws CloneNotSupportedException {return super.clone();}
}
class Human implements Cloneable{public int age;public String name;public Address address;public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Address getAddress() {return address;}public void setAddress(Address address) {this.address = address;}public Object clone() throws CloneNotSupportedException {Human newClone= (Human) super.clone();newClone.address= (Address) address.clone();return newClone;}
}
public class Test15 {public static void main(String[] args) throws CloneNotSupportedException {Address address=new Address();address.setCity("北京");Human human=new Human();human.setAge(10);human.setName("小白");human.setAddress(address);System.out.println("克隆前:");System.out.println(human.getName());System.out.println(human.getAge());System.out.println(human.getAddress().getCity());System.out.println("===============================================");Human humanClone= (Human) human.clone();humanClone.setName("大白");humanClone.setAge(20);Address addressClone= (Address) address.clone();addressClone.setCity("上海");humanClone.setAddress(addressClone);System.out.println("克隆后:");System.out.println(humanClone.getName());System.out.println(humanClone.getAge());System.out.println(humanClone.getAddress().getCity());System.out.println("===============================================");System.out.println("克隆后的引用类型对比");System.out.println("原对象:"+human.getAddress().getCity());System.out.println("拷贝对象:"+humanClone.getAddress().getCity());System.out.println("===============================================");System.out.println("克隆后的基本数据类型对比");System.out.println("原对象:"+human.getName());System.out.println("原对象:"+human.getAge());System.out.println("拷贝对象:"+humanClone.getName());System.out.println("拷贝对象:"+humanClone.getAge());}
}
运行结果只有下图红色圈圈改变了
代码的修改为下面几个图