• java 软硬拷贝


    软拷贝就是复制对象的值;
    硬拷贝是复制这个对象所有的东西,如果该对象引用了其他对象,则引用也会改变到复制的新对象那边。

    软拷贝一般是java.lang.Object 类里面的clone() 方法;
    一般硬拷贝是使用序列化,然后再反析出来;(把对象写到流里面的过程叫序列化过程,从流中读取对象的过程叫反序列化过程)
    transient 修饰的不能序列化,也就不能硬拷贝。

    代码:

    首先是软拷贝的例子:

    public class CloneTest1 {

    public static void main(String[] args) throws CloneNotSupportedException {
    Student student1 = new Student();
    student1.setName("ZhangSan");
    student1.setAge(20);

    Student student2 = new Student();
    student2 = (Student) student1.clone();

    System.out.println("拷贝得到的信息");
    System.out.println(student2.getName());
    System.out.println(student2.getAge());
    System.out.println("-------------");

    // 修改第二个对象的信息
    student2.setName("LiSi");
    student2.setAge(25);

    System.out.println("修改第二个对象的属性为lisi,25后:");
    System.out.println("第一个对象:");
    System.out.println(student1.getName());
    System.out.println(student1.getAge());
    System.out.println("第二个对象:");
    System.out.println(student2.getName());
    System.out.println(student2.getAge());
    System.out.println("-------------");

    // 说明两个引用student1和student2指向的是不同的对象

    }
    }

    class Student implements Cloneable {

    private String name;
    private int age;

    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;
    }

    @Override
    public Object clone() throws CloneNotSupportedException {
    // 注意此处要把protected改为public

    Object object = super.clone();

    return object;
    }
    }

    结果截图:


    硬拷贝栗子:
    代码:

    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.Serializable;

    public class CloneTest3 {

    public static void main(String[] args) throws Exception {
    Teacher3 t = new Teacher3();
    t.setName("Teacher Wang");
    t.setAge(50);

    Student3 s1 = new Student3();
    s1.setAge(20);
    s1.setName("ZhangSan");
    s1.setTeacher(t);

    Student3 s2 = (Student3) s1.deepClone();

    System.out.println("拷贝得到的信息:");
    System.out.println(s2.getName());
    System.out.println(s2.getAge());
    System.out.println(s2.getTeacher().getName());
    System.out.println(s2.getTeacher().getAge());
    System.out.println("---------------------------");

    // 将复制后的对象的老师信息修改一下:
    s2.getTeacher().setName("New Teacher Wang");
    s2.getTeacher().setAge(28);

    System.out.println("修改了拷贝对象的教师后:");
    System.out.println("拷贝对象的教师:");
    System.out.println(s2.getTeacher().getName());
    System.out.println(s2.getTeacher().getAge());
    System.out.println("原来对象的教师:");
    System.out.println(s1.getTeacher().getName());
    System.out.println(s1.getTeacher().getAge());

    // 由此证明序列化的方式实现了对象的深拷贝

    }

    }

    class Teacher3 implements Serializable {

    private String name;
    private int age;

    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;
    }

    }

    class Student3 implements Serializable {

    private String name;
    private int age;
    private Teacher3 teacher;

    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 Teacher3 getTeacher() {
    return teacher;
    }

    public void setTeacher(Teacher3 teacher) {
    this.teacher = teacher;
    }

    public Object deepClone() throws Exception {

    // 序列化
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(bos);

    oos.writeObject(this);

    // 反序列化
    ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
    ObjectInputStream ois = new ObjectInputStream(bis);

    return ois.readObject();
    }

    }


    截图:

  • 相关阅读:
    js 获取当前时间
    html5拨打电话及发短信
    ::before和::after伪元素的使用
    vue单页面应用刷新网页后vuex的state数据丢失问题以及beforeunload的兼容性
    CSS3径向渐变实现优惠券波浪造型
    iOS 幻灯片的自动循环滚动
    iOS 编译正常,但无法运行到真机和模拟器上,Choose a destination with a supported architecture in order to run on this device.
    iOS webView抓取改变js的alertView
    iOS 创建单例的方法
    webView图片点击可以实现预览效果
  • 原文地址:https://www.cnblogs.com/GGboy-wu/p/9677331.html
Copyright © 2020-2023  润新知