日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

CloneNotSupportedException的解決方案 + Object的clone方法分析

作者:Jothan Zhong 更新時間: 2024-01-10 編程語言

CloneNotSupportedException的解決方案

引入問題:

在一次測試clone方法時,D類Override了Object類的clone方法

public class D {

    private Integer A1;
    private Integer A2;

    public D() {
    }

    public D(Integer a1, Integer a2 {
        A1 = a1;
        A2 = a2;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
123456789101112131415161718

在測試類中執行clone方法報錯了

public class Test5 {

    public static void main(String[] args) {

        D p  = new D();
        D p2 = null;
        try {
            p2 = (D)p.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        System.out.println(p == p2);

    }

}
12345678910111213141516

在這里插入圖片描述

拋出了 CloneNotSupportedException

尋找原因:

既然是重寫自Object的clone方法,那么去看看Object對該方法的定義:

protected native Object clone() throws CloneNotSupportedException;
1

其中對于異常拋出的描述如下:

Throws:
CloneNotSupportedException – if the object’s class does not support the Cloneable interface. Subclasses that override the clone method can also throw this exception to indicate that an instance cannot be cloned.

如果對象的class不實現Cloneable接口,子類重寫clone方法會同樣拋出異常表示實例不能被克隆;

哦?

那不意思就是說:你要不父類實現Cloneable接口,要不就子類實現Cloneable

兩個測試代碼:

測試1: 在父類上實現Cloneable接口

public class TestClone {

    public static void main(String[] args) throws CloneNotSupportedException {
        Father father = new Father();
        Father fatherClone = (Father) father.clone();

        Son son = new Son();
        Son sonClone = (Son) son.clone();

        System.out.println(father==fatherClone);
        System.out.println(son==sonClone);

    }

}

class Father implements Cloneable{
    private Integer f1;
    private Integer f2;

    public Father() {
    }

    public Father(Integer f1, Integer f2) {
        this.f1 = f1;
        this.f2 = f2;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}


class Son extends Father{
    private Integer s1;
    private Integer s2;

    public Son() {
    }

    public Son(Integer s1, Integer s2) {
        this.s1 = s1;
        this.s2 = s2;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152

不出意外,沒有任何問題

在這里插入圖片描述

測試2:在子類上實現Cloneable接口

public class TestClone {

    public static void main(String[] args) throws CloneNotSupportedException {
//        Father father = new Father();
//        Father fatherClone = (Father) father.clone();

        Son son = new Son();
        Son sonClone = (Son) son.clone();

//        System.out.println(father==fatherClone);
        System.out.println(son==sonClone);

    }

}

class Father {
    private Integer f1;
    private Integer f2;

    public Father() {
    }

    public Father(Integer f1, Integer f2) {
        this.f1 = f1;
        this.f2 = f2;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}


class Son extends Father implements Cloneable{
    private Integer s1;
    private Integer s2;

    public Son() {
    }

    public Son(Integer s1, Integer s2) {
        this.s1 = s1;
        this.s2 = s2;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152

執行父類的clone報錯,但是執行子類的clone沒得問題

在這里插入圖片描述

SO:

哪個類的需要clone,就在那個類在重寫clone方法,并且在那個類或者其父類中實現Cloneable接口

詳細分析clone方法的實現:

由于Object的clone方法是native修飾的,也就是虛擬機內部實現的方法

根據Hotspot虛擬機定義,找到Object.c

在這里插入圖片描述

為什么要實現Cloneable接口?

在JVM_Clone方法中有這樣一段判斷:

在這里插入圖片描述

很明顯,虛擬機給你拋出的異常

為什么會出現地址不同的情況?

在這里插入圖片描述

很明顯,直接在堆內存中執行allocate方法開辟新的對象空間

并返回一個 oop 對象,也就是對象描述的指針(指向對象的首地址)

原文鏈接:https://blog.csdn.net/qq_43985303/article/details/135315353

  • 上一篇:沒有了
  • 下一篇:沒有了
欄目分類
最近更新