Java里浅克隆和深克隆区别
百度了很多,看着特别晕,有人可以详细讲讲么,谢谢package test;
/*
* 原型设计模式:浅克隆、深克隆
* 创建大量同一类对象,使用克隆:克隆比new效率更高
* Cloneable:空接口,资格的标记
* clone():属于Object类
*/
class Book implements Cloneable{
private String name;
private double price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public Book(String name,double price){
this.name=name;
this.price=price;
}
@Override
public String toString(){
return name+":"+price;
}
@Override
public Object clone() throws CloneNotSupportedException{
return super.clone();
}
}
class Student{
private String name;
private Book book;
}
public class CloneTest {
public static void main(String[] args) throws CloneNotSupportedException {
Book b1 = new Book("luoweideshenlin",26);
Book b2 = (Book)b1.clone();//不等价:b2=b1;
b2.setName("pingfandeshijie");
b2.setPrice(78);
System.out.println(b1.toString());
System.out.println(b2.toString());
System.out.println(b1==b2);
}
}
这个是浅克隆还是深克隆啊,,,,,,,