摘要:類自帶了本地的方法,該方法會(huì)返回現(xiàn)有實(shí)例的副本。如果要使用克隆,必須實(shí)現(xiàn)接口,以便它不會(huì)在運(yùn)行時(shí)拋出。如果函數(shù)會(huì)返回對(duì)象副本,那么在什么情況下我們需要重寫它讓我們運(yùn)行下面的類來(lái)更好的理解。
Java類自帶了本地的clone()方法,該方法會(huì)返回現(xiàn)有實(shí)例的副本。如果要使用Java克隆,必須實(shí)現(xiàn)java.lang.Cloneable接口,以便它不會(huì)在運(yùn)行時(shí)拋出CloneNotSupportedException。
如果clone()函數(shù)會(huì)返回對(duì)象副本,那么在什么情況下我們需要重寫它?
讓我們運(yùn)行下面的java類來(lái)更好的理解。
import java.util.HashMap; import java.util.Iterator; /** * @author 三產(chǎn) * @version 1.0 * @date 2017-03-21 * @QQGroup 213732117 * @website http://www.coderknock.com * @copyright Copyright 2017 拿客 coderknock.com All rights reserved. * @since JDK 1.8 */ public class Clone implements Cloneable { private int id; private String name; private HashMapprops; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public HashMap getProps() { return props; } public void setProps(HashMap props) { this.props = props; } public static void main(String[] args) throws CloneNotSupportedException { Clone ct1 = new Clone(); ct1.setId(1); ct1.setName("first"); HashMap hm = new HashMap(); hm.put("1", "first"); hm.put("2", "second"); hm.put("3", "third"); ct1.setProps(hm); // Using default clone() implementation Clone ct2 = (Clone) ct1.clone(); // Check whether the ct1 and ct2 attributes are same or different System.out.println("ct1 and ct2 HashMap == test: " + (ct1.getProps() == ct2.getProps())); // Lets see the effect of using default cloning ct1.getProps().put("4", "fourth"); System.out.println("ct1 props:" + ct2.getProps()); System.out.println("ct2 props:" + ct1.getProps()); ct1.setName("new"); System.out.println("ct1 name:" + ct1.getName()); System.out.println("ct2 name:" + ct2.getName()); } }
輸出如下:
ct1 and ct2 HashMap == test: true ct1 props:{1=first, 2=second, 3=third, 4=fourth} ct2 props:{1=first, 2=second, 3=third, 4=fourth} ct1 name:new ct2 name:first
很明顯,默認(rèn)clone()函數(shù)使用的是淺復(fù)制的副本,ct2受ct1屬性中的任何更改的影響,所以我們需要覆蓋clone方法,這時(shí)我們反饋clone的注解。
在上面的類中添加下面代碼:
public Clone clone() { System.out.println("invoking overridden clone method"); HashMaphm = new HashMap<>(); String key; Iterator it = this.props.keySet().iterator(); // 深復(fù)制屬性 while (it.hasNext()) { key = it.next(); hm.put(key, this.props.get(key)); } Clone ct = new Clone(); ct.setId(this.id); ct.setName(this.name); ct.setProps(hm); return ct; }
再次運(yùn)行:
ct1 and ct2 HashMap == test: false ct1 props:{1=first, 2=second, 3=third} ct2 props:{1=first, 2=second, 3=third, 4=fourth} ct1 name:new ct2 name:first
這時(shí),我們就可以發(fā)現(xiàn)深復(fù)制與淺復(fù)制的區(qū)別了。
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/66865.html
摘要:有一些設(shè)計(jì)缺陷,其中最大的一個(gè)是接口沒(méi)有方法。這基本上就是你用復(fù)制構(gòu)造函數(shù)做的事情。復(fù)制構(gòu)造方法有幾個(gè)優(yōu)點(diǎn),我在本書中有討論。的方法是非常棘手的。它創(chuàng)建一個(gè)對(duì)象而不調(diào)用構(gòu)造函數(shù)。無(wú)法保證它保留構(gòu)造函數(shù)建立的不變量。 前言 在Java API中,可以通過(guò)實(shí)現(xiàn)Cloneable接口并重寫clone方法實(shí)現(xiàn)克隆,但Java設(shè)計(jì)者否定了使用clone創(chuàng)建新對(duì)象的方法. 1. clone方法實(shí)現(xiàn)...
摘要:不合規(guī)的代碼示例合規(guī)解決方案參閱復(fù)制構(gòu)造函數(shù)與克隆也可以參閱應(yīng)該實(shí)現(xiàn)克隆覆蓋的類應(yīng)為并調(diào)用下面為引文翻譯談設(shè)計(jì)與作者的對(duì)話,作者首次在上發(fā)表,年月日復(fù)制構(gòu)造函數(shù)與克隆在你的書中,你建議使用復(fù)制構(gòu)造函數(shù)而不是實(shí)現(xiàn)和編寫。 今天在用 sonar 審核代碼, 偶然看到下面的提示:showImg(https://segmentfault.com/img/bVbqioZ?w=858&h=116)...
摘要:判斷另外一個(gè)對(duì)象是否與當(dāng)前對(duì)象相等返回當(dāng)前對(duì)象的哈希值返回一個(gè)表示當(dāng)前對(duì)象的字符串喚醒一個(gè)等待當(dāng)前對(duì)象的鎖監(jiān)視器的線程。 原文鏈接:http://www.javacodegeeks.com/2015/09/using-methods-common-to-all-objects.html 本文是Java進(jìn)階課程的第二篇。 本課程的目標(biāo)是幫你更有效的使用Java。其中討論了一些高級(jí)主題,包...
摘要:如果一個(gè)對(duì)象的初始化需要很多其他對(duì)象的數(shù)據(jù)準(zhǔn)備或其他資源的繁瑣計(jì)算,那么可以使用原型模式。當(dāng)需要一個(gè)對(duì)象的大量公共信息,少量字段進(jìn)行個(gè)性化設(shè)置的時(shí)候,也可以使用原型模式拷貝出現(xiàn)有對(duì)象的副本進(jìn)行加工處理。 1、什么是原型模式Specify the kinds of objects to create using a prot...
閱讀 3533·2021-10-14 09:42
閱讀 2801·2021-09-08 10:44
閱讀 1402·2021-09-02 10:18
閱讀 3790·2021-08-30 09:43
閱讀 2905·2021-07-29 13:49
閱讀 3784·2019-08-29 17:02
閱讀 1644·2019-08-29 15:09
閱讀 1091·2019-08-29 11:01