2021-08-04

本文探讨了浅克隆与深克隆在Java中的区别,通过实例展示如何在Student和Phone类中实现浅克隆的简单方式,以及深克隆的复杂过程,包括使用 ByteArrayOutputStream 和 ByteArrayInputStream 进行序列化和反序列化的操作。
摘要由CSDN通过智能技术生成

深浅克隆:
浅克隆只是拷贝了对象当中基本数据类型和字符串。如果对象当中有其他的对象引用是不能拷贝
浅克隆:
//测试类
@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();
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值