java -- clone的学习

clone,简称克隆
克隆 分为深克隆与浅克隆
深克隆:克隆对象及对象中包含的对象
浅克隆:只克隆当前对象中的属性数值,对象中的对象不进行复制

浅克隆事例代码

/*
*Copyright (c) 2016, gp.inc and/or its affiliates. All rights reserved.
*/
package thinkingInJava.thinkingInJava;

public class Snake implements Cloneable {
    private Snake next;
    private char c;

    Snake(int i, char x) {
        c = x;
        if (--i > 0) {
            next = new Snake(i, (char) (x + 1));
        }
    }

    void increment() {
        c++;
        if (next != null) {
            next.increment();
        }
    }

    public String toString() {
        String s = ":" + c;
        if (next != null) {
            s += next.toString();
        }
        return s;
    }

    public Object clone() {
        Object o = null;
        try {
            o = super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return o;
    }

    public static void main(String[] args) {
        Snake s = new Snake(5, 'a');
        System.out.println("No.1 s=" + s);

        // 浅克隆
        Snake s2 = (Snake) s.clone();
        System.out.println("No.2 s2=" + s2);

        s.increment();
        System.out.println("No.3 s=" + s);
        System.out.println("No.4 s2=" + s2);
    }
}

深克隆事例代码

/*
*Copyright (c) 2016, gp.inc and/or its affiliates. All rights reserved.
*/
package thinkingInJava.thinkingInJava;

public class DepthReading implements Cloneable {
    private double depth;

    public DepthReading(double depth) {
        this.depth = depth;
    }

    public Object clone() {
        Object o = null;
        try {
            o = super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return o;
    }

}

class TemperatureReading implements Cloneable {
    private long time;
    private double temperature;

    public TemperatureReading(double temperature) {
        time = System.currentTimeMillis();
        this.temperature = temperature;
    }

    public Object clone() {
        Object o = null;
        try {
            o = super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return o;
    }
}

class OceanReading implements Cloneable {
    private DepthReading depth;
    private TemperatureReading temperature;

    public OceanReading(double tdata, double ddata) {
        depth = new DepthReading(ddata);
        temperature = new TemperatureReading(tdata);
    }

    public Object clone() {
        OceanReading o = null;
        try {
            o = (OceanReading) super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }

        o.depth = (DepthReading) o.depth.clone();
        o.temperature = (TemperatureReading) o.temperature.clone();

        return o;
    }
}

代码核心注意62 63 行

/*
*Copyright (c) 2016, gp.inc and/or its affiliates. All rights reserved.
*/
package thinkingInJava.thinkingInJava;

public class DeepCopy {
    public static void main(String[] args) {
        OceanReading reading = new OceanReading(33.9, 100.5);
        OceanReading r = (OceanReading) reading.clone();
    }
}

首先构造基础类,然后是下一个衍生的构建器⋯⋯以此类推,直到位于最深层的衍生构建器。
区别在于clone()并不是个构建器,所以没有办法实现自动克隆。为了克隆,必须由自己明确进行。

通过序列化复制对象与clone对象比较

通过序列化对象进行复制,与通过克隆对象创建对象,相比,序列化的性能低效,克隆性能高效

事例代码

/*
*Copyright (c) 2016, gp.inc and/or its affiliates. All rights reserved.
*/
package thinkingInJava.thinkingInJava;

import java.io.Serializable;

public class Thing1 implements Serializable {

}

class Thing2 implements Serializable {
    Thing1 t = new Thing1();
}

class Thing3 implements Cloneable {
    public Object clone() {
        Object o = null;
        try {
            o = super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return o;
    }
}

class Thing4 implements Cloneable {
    Thing3 t3 = new Thing3();

    public Object clone() {
        Thing4 o = null;
        try {
            o = (Thing4) super.clone();
        } catch (CloneNotSupportedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        o.t3 = (Thing3) t3.clone();

        return o;
    }
}
/*
*Copyright (c) 2016, gp.inc and/or its affiliates. All rights reserved.
*/
package thinkingInJava.thinkingInJava;

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

public class TestThing1 {

    static final int SIZE = 5000;

    public static void main(String[] args) {
        Thing2[] a = new Thing2[SIZE];
        for (int i = 0; i < a.length; i++) {
            a[i] = new Thing2();
        }

        Thing4[] b = new Thing4[SIZE];
        for (int i = 0; i < b.length; i++) {
            b[i] = new Thing4();
        }

        try {
            long t1 = System.currentTimeMillis();
            ByteArrayOutputStream buf = new ByteArrayOutputStream();
            ObjectOutputStream o = new ObjectOutputStream(buf);
            for (int i = 0; i < a.length; i++) {
                o.writeObject(a[i]);
            }

            ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buf.toByteArray()));

            Thing2[] c = new Thing2[SIZE];
            for (int i = 0; i < c.length; i++) {
                try {
                    c[i] = (Thing2) in.readObject();
                } catch (ClassNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

            long t2 = System.currentTimeMillis();
            System.out.println((t2 - t1) + " milliseconds");

            t1 = System.currentTimeMillis();

            Thing4[] d = new Thing4[SIZE];
            for (int i = 0; i < d.length; i++) {
                d[i] = (Thing4) b[i].clone();
            }
            t2 = System.currentTimeMillis();
            System.out.println((t2 - t1) + " milliseconds");


        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值