深浅克隆:
浅克隆只是拷贝了对象当中基本数据类型和字符串。如果对象当中有其他的对象引用是不能拷贝
浅克隆:
//测试类
@SuppressWarnings(“all”)
public class Test {
public static void main(String[] args) throws Exception {
//创建学生类的对象
Student one = new Student("迪丽热巴",23,new Phone("IPhoneXX",300));
//克隆对象
Student two = (Student) one.clone();
System.out.println("one = " + one);
System.out.println("two = " + two);
System.out.println("=============");
one.show();
System.out.println("-------");
two.show();
}
}
定义一个学生类:
//学生类
@SuppressWarnings(“all”)
public class Student implements Cloneable{
private String name;
private int age;
private Phone p;
//重写方法。来自于Object类的克隆 clone方法,快捷键 Ctrl+o (欧)
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
public Student(String name, int age, Phone p) {
this.name = name;
this.age = age;
this.p = p;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Phone getP() {
return p;
}
public void setP(Phone p) {
this.p = p;
}
//定义普通方法
public void show(){
System.out.println("p = " + p);
System.out.println("p.getBrand() = " + p.getBrand());
System.out.println("p.getPrice() = " + p.getPrice());
System.out.println("name = " + name);
System.out.println("age = " + age);
}
}
//手机类
@SuppressWarnings(“all”)
public class Phone {
private String brand;
private int price;
public Phone(String brand, int price) {
this.brand = brand;
this.price = price;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
}
深克隆:
//手机类
@SuppressWarnings(“all”)
public class Phone implements Serializable {
private String brand;
private int price;
public Phone(String brand, int price) {
this.brand = brand;
this.price = price;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
}
//学生类
@SuppressWarnings(“all”)
public class Student implements Serializable,Cloneable{
private String name;
private int age;
private Phone p;
//重写方法。来自于Object类的克隆 clone方法,快捷键 Ctrl+o (欧)
//深克隆的方式:
//将对象 ---写入到内存---> 内存 ---读取处理---> 新的对象
//ByteArrayOutputStream 和 ByteArrayInputStream
//ByteArrayOutputStream --> 写数据到数组里面(存在于内存当中)
//ByteArrayInputStream ---> 读取上面数组的内容即可
@Override
protected Object clone() throws CloneNotSupportedException {
//定义对象的引用
Student stu = null;
try {
//1.将当前的对象,写入到内存当中。
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
//写对象,谁调用 clone()方法,我就写谁。
oos.writeObject(this);
//2.重新读取数据,读取写入的内存数据,数据来自于 bos
//在 bos 当中保存的就是 oos 里面写的对象 this
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bis);
Object o = ois.readObject();
//3. 强制类型转换
stu = (Student)o;
} catch (Exception e) {
e.printStackTrace();
}
//直接返回
return stu;
}
public Student(String name, int age, Phone p) {
this.name = name;
this.age = age;
this.p = p;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Phone getP() {
return p;
}
public void setP(Phone p) {
this.p = p;
}
//定义普通方法
public void show(){
System.out.println("p = " + p);
System.out.println("p.getBrand() = " + p.getBrand());
System.out.println("p.getPrice() = " + p.getPrice());
System.out.println("name = " + name);
System.out.println("age = " + age);
}
}
//测试类
@SuppressWarnings(“all”)
public class Test {
public static void main(String[] args) throws Exception {
//创建学生类的对象
Student one = new Student("迪丽热巴",23,new Phone("IPhoneXX",300));
//克隆对象
Student two = (Student) one.clone();
System.out.println("one = " + one);
System.out.println("two = " + two);
System.out.println("=============");
one.show();
System.out.println("-------");
two.show();
}
}
深克隆:
//重写方法。来自于Object类的克隆 clone方法,快捷键 Ctrl+o (欧)
//深克隆的方式:
//将对象 —写入到内存—> 内存 —读取处理—> 新的对象
//ByteArrayOutputStream 和 ByteArrayInputStream
//ByteArrayOutputStream --> 写数据到数组里面(存在于内存当中)
//ByteArrayInputStream —> 读取上面数组的内容即可
@Override
protected Object clone() throws CloneNotSupportedException {
//定义对象的引用
Student stu = null;
try {
//1.将当前的对象,写入到内存当中。
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
//写对象,谁调用 clone()方法,我就写谁。
oos.writeObject(this);
//2.重新读取数据,读取写入的内存数据,数据来自于 bos
//在 bos 当中保存的就是 oos 里面写的对象 this
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bis);
Object o = ois.readObject();
//3. 强制类型转换
stu = (Student)o;
} catch (Exception e) {
e.printStackTrace();
}
//直接返回
return stu;
}
浅克隆:
//重写方法。来自于Object类的克隆 clone方法,快捷键 Ctrl+o (欧)
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}