java Clone接口和深拷貝詳解
對(duì)于數(shù)組的拷貝,如果是簡(jiǎn)單類型的話是深拷貝,如果是引用類型的話是淺拷貝,但是因?yàn)閖ava是面向?qū)ο蟮模诨卮鹈嬖嚬賳栴}的時(shí)候,我們可以不用說的這么細(xì),可以直接說淺拷貝。
代碼示例1
class Person implements Cloneable{//如果想克隆自定義類,那么需要在自定義類上實(shí)現(xiàn)Cloneable接口
public int age;
/*疑問:為什么這個(gè)接口是空接口呢?這是一個(gè)面試問題。
空節(jié)課:也把它叫做標(biāo)記接口。其實(shí)就是這個(gè)意思:只要一個(gè)類實(shí)現(xiàn)了這個(gè)接口,那么就標(biāo)記這個(gè)類是可以進(jìn)行clone的
*
* 2:重寫clone方法*/
@Override
protected Object clone() throws CloneNotSupportedException {//重寫了父類的克隆方法
return super.clone();
}
}
public class TestDemo {
public static void main(String[] args) throws CloneNotSupportedException {
Person person1 = new Person();
Person person2 = (Person) person1.clone();
System.out.println(person1.age);
System.out.println(person2.age);
System.out.println("=======修改=======");
person2.age = 99;
System.out.println(person1.age);
System.out.println(person2.age);
}
/*public static void main(String[] args) {
int[] array = {1,2,3,4,5,6};
int[] array2 = array.clone();//對(duì)這個(gè)數(shù)組進(jìn)行克隆
array2[0] = 33;//改變拷貝后的數(shù)組元素的值不會(huì)影響原來數(shù)組的元素,這種情況是深拷貝
System.out.println(Arrays.toString(array2));
System.out.println(Arrays.toString(array));
}*/
}
輸出為:

因?yàn)楦淖兊氖呛?jiǎn)單類型,所以這種情況是深拷貝。
代碼示例2
class Money{
double money = 12.5;
}
class Person implements Cloneable{//如果想克隆自定義類,那么需要在自定義類上實(shí)現(xiàn)Cloneable接口
public int age;
/*疑問:為什么這個(gè)接口是空接口呢?這是一個(gè)面試問題。
空節(jié)課:也把它叫做標(biāo)記接口。其實(shí)就是這個(gè)意思:只要一個(gè)類實(shí)現(xiàn)了這個(gè)接口,那么就標(biāo)記這個(gè)類是可以進(jìn)行clone的
*
* 2:重寫clone方法*/
Money m = new Money();
@Override
protected Object clone() throws CloneNotSupportedException {//重寫了父類的克隆方法
return super.clone();
}
}
public class TestDemo {
public static void main(String[] args) throws CloneNotSupportedException {
Person person1 = new Person();
Person person2 = (Person) person1.clone();
System.out.println(person1.m.money);
System.out.println(person2.m.money);
System.out.println("=========修改==========");
person2.m.money = 99.9;
System.out.println(person1.m.money);
System.out.println(person2.m.money);
}
}
輸出為:

可以參考以下圖分析:

這種情況就是淺拷貝,那么可以將這個(gè)淺拷貝變成深拷貝嗎?只需要將Money也克隆一下
class Money implements Cloneable{//如果想要變成深拷貝的話,那么money也需要被克隆。
double money = 12.5;
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
class Person implements Cloneable{//如果想克隆自定義類,那么需要在自定義類上實(shí)現(xiàn)Cloneable接口
public int age;
Money m = new Money();
@Override
protected Object clone() throws CloneNotSupportedException {//重寫了父類的克隆方法
Person p = (Person)super.clone();//1:將當(dāng)前的對(duì)象克隆一份,克隆person
p.m = (Money) this.m.clone();//2:克隆當(dāng)前的Money對(duì)象
return p;
}
}
public class TestDemo {
public static void main(String[] args) throws CloneNotSupportedException {
Person person1 = new Person();
Person person2 = (Person) person1.clone();
System.out.println(person1.m.money);
System.out.println(person2.m.money);
System.out.println("=========修改==========");
person2.m.money = 99.9;
System.out.println(person1.m.money);
System.out.println(person2.m.money);
}
}

這樣就就將淺拷貝轉(zhuǎn)變成了深拷貝,可以參考以下圖分析:

總結(jié)
本篇文章就到這里了,希望能給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
Mybatis?Interceptor線程安全引發(fā)的bug問題
這篇文章主要介紹了Mybatis?Interceptor線程安全引發(fā)的bug問題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02
SpringBoot基于Mybatis-Plus自動(dòng)代碼生成
這篇文章主要介紹了SpringBoot基于Mybatis-Plus自動(dòng)代碼生成,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
關(guān)于Https協(xié)議和HttpClient的實(shí)現(xiàn)詳解
這篇文章主要給大家介紹了關(guān)于Https協(xié)議和HttpClient實(shí)現(xiàn)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-05-05
IntelliJ IDEA 使用經(jīng)驗(yàn)總結(jié)(推薦)
這篇文章主要介紹了IntelliJ IDEA 使用經(jīng)驗(yàn)總結(jié),非常不錯(cuò),具有參考價(jià)值,需要的朋友可以參考下2018-02-02
web中拖拽排序和java后臺(tái)交互實(shí)現(xiàn)方法示例
這篇文章主要給大家介紹了關(guān)于web中拖拽排序和java后臺(tái)交互實(shí)現(xiàn)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-12-12
nacos配置中心遠(yuǎn)程調(diào)用讀取不到配置文件的解決
這篇文章主要介紹了nacos配置中心遠(yuǎn)程調(diào)用讀取不到配置文件的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。2022-01-01
MyBatis工廠類封裝與簡(jiǎn)化實(shí)現(xiàn)
工廠類的目的是將對(duì)象的創(chuàng)建邏輯封裝在一個(gè)類中,以便客戶端代碼無需了解具體的實(shí)現(xiàn)細(xì)節(jié),本文主要介紹了MyBatis工廠類封裝與簡(jiǎn)化實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2024-01-01

