【面试】--java 基础Cloneable 最彻底的clone是什么?

37 篇文章 0 订阅

1 默认数组类型 引用类型都是浅copy

The method {@code clone} for class {@code Object} performs a
* specific cloning operation. First, if the class of this object does
* not implement the interface {@code Cloneable}, then a
* {@code CloneNotSupportedException} is thrown. Note that all arrays
* are considered to implement the interface {@code Cloneable} and that
* the return type of the {@code clone} method of an array type {@code T[]}
* is {@code T[]} where T is any reference or primitive type.
* Otherwise, this method creates a new instance of the class of this
* object and initializes all its fields with exactly the contents of
* the corresponding fields of this object, as if by assignment; the
* contents of the fields are not themselves cloned. Thus, this method
* performs a "shallow copy" of this object, not a "deep copy" operation.

2 代码实例

public class CloneTest implements Cloneable {
    int count;
    CloneTest next;
    List<Integer> list = Lists.newArrayList();



    public CloneTest(int count) {
        list.add(count);
        this.count = count;
        if (count > 0)
            next = new CloneTest(count - 1);
    }

    void add() {
        count++;
        list.add(count);
        if (next != null)
            next.add();
    }

    public String toString() {
        String s = String.valueOf(count) + " ";
        if (next != null)
            s += next.toString();
        return s+ JSON.toJSONString(list);
    }

    //   深拷贝
    public Object clone() {
        Object o = null;
        o = new CloneTest(count);
        return o;
    }

    //浅拷贝
    @Override
    public Object clone() {
        Object o = null;
        try {
            o = super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return o;
    }

    public static void main(String[] args) {
        CloneTest t = new CloneTest(3);
        System.out.println("t=" + t);
        CloneTest t1 = (CloneTest) t.clone();
        System.out.println("t1=" + t1);
        t.add();
        System.out.println(t.next == t1.next);
        System.out.println("after added\nt t=" + t + "\nt1=" + t1);
    }
}

3 结果

t=3 2 1 0 [0][1][2][3]
t1=3 2 1 0 [0][1][2][3]
true
after added
t t=4 3 2 1 [0,1][1,2][2,3][3,4]
t1=3 3 2 1 [0,1][1,2][2,3][3,4]

深拷贝

t=3 2 1 0 [0][1][2][3]
t1=3 2 1 0 [0][1][2][3]
false
after added
t t=4 3 2 1 [0,1][1,2][2,3][3,4]
t1=3 2 1 0 [0][1][2][3]

4 阿里巴巴 JSONObject 实现的clone

public Object clone() {
    return new JSONObject((Map)(this.map instanceof LinkedHashMap?new LinkedHashMap(this.map):new HashMap(this.map)));
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

自驱

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值