java clone按clone效果分为浅克隆和深克隆;
浅克隆是指克隆之后,如果克隆对象改变了引用变量,会导致被克隆对象的引用变量属性值发生变化;而深克隆不会带来这种改变;
对于基本数据类型的属性(包括string数据类型)不管是浅克隆还是深克隆都不会引起被克隆对象属性值的改变
1.浅克隆用法定义一个Address类
public class Address{
public String country;
public String province;
public String city;
public Address(String country, String province, String city) {
super();
this.country = country;
this.province = province;
this.city = city;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
@Override
public String toString() {
// TODO 自动生成的方法存根
return country + "/" + province + "/" + city ;
}
}
定义一个Student类,并实现Cloneable接口
public class Student implements Cloneable{
public String name;
public int age;
public Address address;
public Student(String name, int age, Address address) {
super();
this.name = name;
this.age = age;
this.address = address;
}
@Override
protected Object clone(){
// TODO 自动生成的方法存根
Student student = null;
try {
student = (Student)super.clone();
} catch (CloneNotSupportedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return student;
}
@Override
public String toString() {
// TODO 自动生成的方法存根
return "name: " + name + ";age= " + age + ";address:" + address;
}
}
测试
public class TestMain {
public static void main(String[] args) {
// TODO 自动生成的方法存根
Address address = new Address("china","shanxi","ankang");
Student s1 = new Student("xiaoming", 26, address);
Student s2 = (Student)s1.clone();
System.out.println("clone before s1:" + s1);
s2.name = "xiaohua";
s2.age = 25;
s2.address.setCountry("china");
s2.address.setProvince("shanxi");
s2.address.setCity("weinan");
System.out.println("clone after s1:" + s1);
System.out.println("clone after s2:" + s2);
}
}
测试结果
clone before s1:name: xiaoming;age= 26;address:china/shanxi/ankang
clone after s1:name: xiaoming;age= 26;address:china/shanxi/weinan
clone after s2:name: xiaohua;age= 25;address:china/shanxi/weinan
结果分析:克隆对象改变其引用类型属性值后引起被克隆对象属性值的改变,属于浅克隆
2.深克隆用法
定义一个Address类,并实现Cloneable接口
public class Address implements Cloneable{
public String country;
public String province;
public String city;
public Address(String country, String province, String city) {
super();
this.country = country;
this.province = province;
this.city = city;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
@Override
protected Object clone() {
// TODO 自动生成的方法存根
Address address = null;
try {
address = (Address)super.clone();
} catch (CloneNotSupportedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return address;
}
@Override
public String toString() {
// TODO 自动生成的方法存根
return country + "/" + province + "/" + city ;
}
}
定义一个Student类,并实现Cloneable接口
public class Student implements Cloneable{
public String name;
public int age;
public Address address;
public Student(String name, int age, Address address) {
super();
this.name = name;
this.age = age;
this.address = address;
}
@Override
protected Object clone(){
// TODO 自动生成的方法存根
Student student = null;
try {
student = (Student)super.clone();
student.address = (Address)address.clone();
} catch (CloneNotSupportedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return student;
}
@Override
public String toString() {
// TODO 自动生成的方法存根
return "name: " + name + ";age= " + age + ";address:" + address;
}
}
测试
public class TestMain {
public static void main(String[] args) {
// TODO 自动生成的方法存根
Address address = new Address("china","shanxi","ankang");
Student s1 = new Student("xiaoming", 26,address);
Student s2 = (Student)s1.clone();
System.out.println("clone before s1:" + s1);
s2.name = "xiaohua";
s2.age = 25;
s2.address.setCountry("china");
s2.address.setProvince("shanxi");
s2.address.setCity("weinan");
System.out.println("clone after s1:" + s1);
System.out.println("clone after s2:" + s2);
}
}
测试结果
clone before s1:name: xiaoming;age= 26;address:china/shanxi/ankang
clone after s1:name: xiaoming;age= 26;address:china/shanxi/ankang
clone after s2:name: xiaohua;age= 25;address:china/shanxi/weinan
结果分析:克隆对象改变其引用类型属性值后没有引起被克隆对象属性值的改变,属于深克隆