对象假Clone
讲讲为什么会有假clone
首先要明白基本类型(int,float ,double等) 和引用变量(指向对象的地址)保存在栈中,对象保存在堆中。
对于引用变量而言,使用 = 将修改引用 也就是增加一个引用地址,记住不是复制堆中的对象,他们的两个引用地址都是指向同一个对象。
总结: 通常情况下,很少使用 = 来操作对象,也很少用 == 比较两个对象 。 引用类型用双等于号基本上都是False。
下边我简单写个类,验证上边所说的
public class HelloWorld {
int age;
public HelloWorld(int age) {
this.age=age;
}
public static void main(String[] args) {
HelloWorld a=new HelloWorld(10);
HelloWorld b=a;
b.age=2;
System.out.println(a.age);
}
}
打印结果: 2 , 已经验证了我们的结果。
浅克隆
克隆解决的场景:
例如: 要获得一个非常复杂的对象,与其使用new创建该对象再对各个属性进行赋值,不如直接clone现有的对象。
通俗的说浅克隆 : 只clone一层引用,对象的变量如果是引用类型 ,clone 的时候就不能覆盖,只能用深clone ,这也是浅深clone 的区别 。
咱们知道 如果没有父类,默认是Object类,Object类中有很多实用的方法, 比如 equals, toString 当然了 也有一个clone对象的方法, 注意 该方法是一个受保护的,需要重写这个方法。
简单的实例看看 如果变量是引用类型会出问题。具体也不能说问题,适合自己的场景也算是对的。
class Address{ //声明一个address 类
String city;
public Address(String city){
this.city=city;
}
}
public class HelloWorld implements Cloneable { // 实现Clonealbe接口,否则会报错
int age;
Address address ; //address 是一个对象
public HelloWorld(int age,Address address) {
this.age=age;
this.address=address;
}
@Override
public HelloWorld clone() { //实现一个浅clone
HelloWorld a=null;
try {
a=(HelloWorld)super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return a;
}
public static void main(String[] args) {
Address address=new Address("shanghai");
HelloWorld a=new HelloWorld(10,address);
HelloWorld b=a.clone();
b.address.city="beijing";
b.age=18;
System.out.println(a.address.city);
System.out.println(a.age);
}
}
打印结果:
beijing // 修改成功
10 // 不能修改
总结: 对于类的每个属性,如果只是包含,基本类型 和不可变的引用类型,如String , 或者对象再起声明周期内不会发生变化, 则可以使用浅克隆来复制对象, 通常情况下,这样的情况不会一直存在, 所以还是用下边的深克隆吧
深clone
浅克隆之所以不能完全clone,是因为浅clone 有对象属性的时候,只能clone 对象引用,所以clone 前的对象改变了, clone后的对象也要改变, 解决方法是重写属性对象的clone 方法,比较下边和上边的例子
class Address implements Cloneable{ //声明一个address 类
String city;
public Address(String city){
this.city=city;
}
public Address clone() throws CloneNotSupportedException { //重写了clone方法
Address A=null;
A=(Address)super.clone();
return A;
}
}
public class HelloWorld implements Cloneable { // 实现Clonealbe接口,否则会报错
int age;
Address address ; //address 是一个对象
public HelloWorld(int age,Address address) {
this.age=age;
this.address=address;
}
@Override
public HelloWorld clone() { //实现一个浅clone
HelloWorld a=null;
try {
a=(HelloWorld)super.clone();
a.address=address.clone();// 调用了属性对象的clone方法。
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return a;
}
public static void main(String[] args) {
Address address=new Address("shanghai");
HelloWorld a=new HelloWorld(10,address);
HelloWorld b=a.clone();
b.address.city="beijing";
b.age=18;
System.out.println(a.address.city);
System.out.println(a.age);
}
}
打印结果:
shanghai
10
序列化与对象克隆
对于深克隆而言,如果类有很多引用类型的属性,那么重写clone()方法依次复制各个属性是非常麻烦的, 应该考虑使用序列化的方式实现深clone。
class Address implements Serializable { //声明一个address 类
String city;
public Address(String city){
this.city=city;
}
public Address clone() throws CloneNotSupportedException { //重写了clone方法
Address A=null;
A=(Address)super.clone();
return A;
}
}
public class HelloWorld implements Cloneable,Serializable { // 实现Clonealbe接口,否则会报错
int age;
Address address ; //address 是一个对象
public HelloWorld(int age,Address address) {
this.age=age;
this.address=address;
}
@Override
public HelloWorld clone() { //实现一个浅clone
HelloWorld a=null;
ByteArrayOutputStream baos=new ByteArrayOutputStream();
try {
ObjectOutputStream b=new ObjectOutputStream(baos);
b.writeObject(this);
b.close();
} catch (IOException e) {
e.printStackTrace();
}
ByteArrayInputStream c=new ByteArrayInputStream(baos.toByteArray());
try {
ObjectInputStream d=new ObjectInputStream(c);
a=(HelloWorld)d.readObject();
c.close();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
return a;
}
public static void main(String[] args) {
Address address=new Address("shanghai");
HelloWorld a=new HelloWorld(10,address);
HelloWorld b=a.clone();
b.address.city="beijing";
b.age=18;
System.out.println(a.address.city);
System.out.println(a.age);
}
}
总结:
对于任何一个序列化的对象,都要求实现 Serializalble 接口,如果该类的属性有引用类型,则要求该引用类型也要实现Serializable 接口,依次类推 ,最后序列化方式实现克隆会比直接克隆各个引用类型属性慢,这一点在效率优先时要考虑