深拷贝,浅拷贝的方式

对象中只有基本数据类型。实现cloneable接口。实现先拷贝。

例子:

public class Grmmer {
    public static class  Professor0 implements Cloneable {

        String name;
        int age;

        Professor0(String name, int age) {
            this.name = name;
            this.age = age;
        }

        public Object clone() throws CloneNotSupportedException {
            return super.clone();
        }
    }

   static class Student0 implements Cloneable {
        String name;// 常量对象。
        int age;
        Professor0 p;// 学生1和学生2的引用值都是一样的。

        Student0(String name, int age, Professor0 p) {
            this.name = name;
            this.age = age;
            this.p = p;
        }

        public Object clone() {
            Student0 o = null;
            try {
                o = (Student0) super.clone();
            } catch (CloneNotSupportedException e) {
                System.out.println(e.toString());
            }
            return o;
        }
    }

    public static void main(String[] args) {
        Professor0 p = new Professor0("wangwu", 50);
        Student0 s1 = new Student0("zhangsan", 18, p);
        Student0 s2 = (Student0) s1.clone();
        s2.p.name = "lisi";
        s2.p.age = 30;
        s2.name = "z";
        s2.age = 45;
        System.out.println("学生s1的姓名:" + s1.name + "\n学生s1教授的姓名:" + s1.p.name + "," + "\n学生s1教授的年纪" + s1.p.age);// 学生1的教授
    }
}

 

对象中只有引用数据类型。实现序列化 Serializable 接口实现深拷贝。

例子:

public class DeepClone {
   static class Professor2 implements Serializable {
        /**
         *
         */
        private static final long serialVersionUID = 1L;
        String name;
        int age;

        Professor2(String name, int age) {
            this.name = name;
            this.age = age;
        }
    }

   static class Student2 implements Serializable {
        /**
         *
         */
        private static final long serialVersionUID = 1L;
        String name;// 常量对象。
        int age;
        Professor2 p;// 学生1和学生2的引用值都是一样的。

        Student2(String name, int age, Professor2 p) {
            this.name = name;
            this.age = age;
            this.p = p;
        }

        public Object deepClone() throws IOException, OptionalDataException,
                ClassNotFoundException {
            // 将对象写到流里
            ByteArrayOutputStream bo = new ByteArrayOutputStream();
            ObjectOutputStream oo = new ObjectOutputStream(bo);
            oo.writeObject(this);
            // 从流里读出来
            ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());
            ObjectInputStream oi = new ObjectInputStream(bi);
            return (oi.readObject());
        }

       public static void main(String[] args) throws Exception {
           long t1 = System.currentTimeMillis();
           Professor2 p = new Professor2("wangwu", 50);
           Student2 s1 = new Student2("zhangsan", 18, p);
           Student2 s2 = (Student2) s1.deepClone();
           s2.p.name = "lisi";
           s2.p.age = 30;
           System.out.println("name=" + s1.p.name + "," + "age=" + s1.p.age); // 学生1的教授不改变。
           long t2 = System.currentTimeMillis();
           System.out.println(t2-t1);
       }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值