淺談Java中實(shí)現(xiàn)深拷貝的兩種方式—clone() & Serialized
clone() 方法麻煩一些,需要將所有涉及到的類實(shí)現(xiàn)聲明式接口 Cloneable,并覆蓋Object類中的clone()方法,并設(shè)置作用域?yàn)閜ublic(這是為了其他類可以使用到該clone方法)。
序列化的方法簡(jiǎn)單,需要將所有涉及到的類實(shí)現(xiàn)接口Serializable
package b1ch06.clone;
import java.io.Serializable;
class Car implements Cloneable, Serializable {
private String band;
public Car(String band) {
this.band = band;
}
public String getBand() {
return band;
}
public void setBand(String band) {
this.band = band;
}
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
package b1ch06.clone;
import java.io.Serializable;
class Employee implements Cloneable, Serializable {
private String name;
private Car car;
public Employee(String name, Car car) {
this.name = name;
this.car = car;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Car getcar() {
return car;
}
public void setcar(Car car) {
this.car = car;
}
protected void test() {
System.out.println("test func");
}
@Override
public Object clone() throws CloneNotSupportedException {
Employee employee_cloned = (Employee) super.clone();
Car car_cloned = (Car) this.car.clone();
employee_cloned.setcar(car_cloned);
return employee_cloned;
}
}
package b1ch06.clone;
import java.io.*;
public class SerializedClone {
@SuppressWarnings("unchecked")
public static <T extends Serializable> T clone(T obj) {
T cloneObj = null;
try {
//寫入字節(jié)流
ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream obs = new ObjectOutputStream(out);
obs.writeObject(obj);
obs.close();
//分配內(nèi)存,寫入原始對(duì)象,生成新對(duì)象
ByteArrayInputStream ios = new ByteArrayInputStream(out.toByteArray());
ObjectInputStream ois = new ObjectInputStream(ios);
//返回生成的新對(duì)象
cloneObj = (T) ois.readObject();
ois.close();
} catch (Exception e) {
e.printStackTrace();
}
return cloneObj;
}
}
package b1ch06.clone;
public class MyClone {
public static void main(String[] args) {
Car car = new Car("BMW");
Employee employee = new Employee("ANDY", car);
// 方法一:覆蓋所有涉及到的類的clone()方法
try {
Employee employee_cp = (Employee) employee.clone();
System.out.println("=========================");
System.out.println("original對(duì)象地址?:");
System.out.println(employee.toString());
System.out.println("copy對(duì)象地址?:");
System.out.println(employee_cp.toString());
System.out.println("前后兩個(gè)對(duì)象指向同一地址?:");
System.out.println(employee_cp == employee);
System.out.println("=========================");
System.out.println("original對(duì)象中car對(duì)象地址?:");
System.out.println(employee.getcar().toString());
System.out.println("copy對(duì)象中car對(duì)象地址?:");
System.out.println(employee_cp.getcar().toString());
System.out.println("前后兩個(gè)car對(duì)象指向同一地址?:");
System.out.println(employee_cp == employee);
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
// 方法二:序列化實(shí)現(xiàn)深拷貝
Employee cloned_employee = SerializedClone.clone(employee);
System.out.println("=========================");
System.out.println("original對(duì)象地址?:");
System.out.println(employee.toString());
System.out.println("copy對(duì)象地址?:");
System.out.println(cloned_employee.toString());
System.out.println("前后兩個(gè)對(duì)象指向同一地址?:");
System.out.println(cloned_employee == employee);
System.out.println("=========================");
System.out.println("original對(duì)象中car對(duì)象地址?:");
System.out.println(employee.getcar().toString());
System.out.println("copy對(duì)象中car對(duì)象地址?:");
System.out.println(cloned_employee.getcar().toString());
System.out.println("前后兩個(gè)car對(duì)象指向同一地址?:");
System.out.println(cloned_employee == employee);
}
}
以上所述是小編給大家介紹的Java中實(shí)現(xiàn)深拷貝的兩種方式--——clone() & Serialized詳解整合,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
詳解spring boot jpa整合QueryDSL來(lái)簡(jiǎn)化復(fù)雜操作
這篇文章主要介紹了詳解spring boot jpa整合QueryDSL來(lái)簡(jiǎn)化復(fù)雜操作,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2018-04-04
JAVA8 List<List<Integer>> list中再裝一個(gè)list轉(zhuǎn)成一個(gè)list操
這篇文章主要介紹了JAVA8 List<List<Integer>> list中再裝一個(gè)list轉(zhuǎn)成一個(gè)list操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2020-08-08
Jpa數(shù)據(jù)操作以及@Query和@Modifying注解使用方式
這篇文章主要介紹了Jpa數(shù)據(jù)操作以及@Query和@Modifying注解使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
java web開發(fā)中大量數(shù)據(jù)導(dǎo)出Excel超時(shí)(504)問題解決
開發(fā)測(cè)試時(shí)候?qū)霐?shù)據(jù)遇到大數(shù)據(jù)導(dǎo)入的問題,整理了下,需要的朋友可以參考下2017-04-04
Spring的CorsFilter會(huì)失效的原因及解決方法
眾所周知CorsFilter是Spring提供的跨域過濾器,我們可能會(huì)做以下的配置,基本上就是允許任何跨域請(qǐng)求,我利用Spring的CorsFilter做跨域操作但是出現(xiàn)報(bào)錯(cuò),接下來(lái)小編就給大家介紹一Spring的CorsFilter會(huì)失效的原因及解決方法,需要的朋友可以參考下2023-09-09
java項(xiàng)目依賴包選擇具體實(shí)現(xiàn)類示例介紹
這篇文章主要為大家介紹了java項(xiàng)目依賴包選擇具體實(shí)現(xiàn)類示例介紹,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
Java編程之多線程死鎖與線程間通信簡(jiǎn)單實(shí)現(xiàn)代碼
這篇文章主要介紹了Java編程之多線程死鎖與線程間通信簡(jiǎn)單實(shí)現(xiàn)代碼,具有一定參考價(jià)值,需要的朋友可以了解下。2017-10-10
Java中@JSONField和@JsonProperty注解的用法及區(qū)別詳解
@JsonProperty和@JSONField注解都是為了解決obj轉(zhuǎn)json字符串的時(shí)候,將java bean的屬性名替換成目標(biāo)屬性名,下面這篇文章主要給大家介紹了關(guān)于Java中@JSONField和@JsonProperty注解的用法及區(qū)別的相關(guān)資料,需要的朋友可以參考下2024-06-06

