博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java设计模式 - 原型模式 prototype
阅读量:5047 次
发布时间:2019-06-12

本文共 1240 字,大约阅读时间需要 4 分钟。

原型模式中的拷贝分为"浅拷贝"和"深拷贝":

浅拷贝: 对值类型的成员变量进行值的复制,对引用类型的成员变量只复制引用,不复制引用的对象.

深拷贝: 对值类型的成员变量进行值的复制,对引用类型的成员变量也进行引用对象的复制.

关于Object类的clone方法:

The method clone for class Object performs a specific cloning operation. First, if the class of this object does not implement the interface Cloneable, then a CloneNotSupportedException is thrown. Note that all arrays are considered to implement the interface Cloneable. 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. 

默认实现为“浅拷贝”,请注意。

实现“深拷贝”时,clone()方法中该类的引用类型的成员变量要给个新对象。

关于java.lang.Cloneable接口:

A class implements the Cloneable interface to indicate to the java.lang.Object.clone() method that it is legal for that method to make a field-for-field copy of instances of that class.

Invoking Object's clone method on an instance that does not implement the Cloneable interface results in the exception CloneNotSupportedException being thrown. 

转载于:https://www.cnblogs.com/brillian-Jin/archive/2013/03/18/2966287.html

你可能感兴趣的文章
【题解】 [ZJOI2006]书架 (Splay)
查看>>
Django ORM那些相关操作
查看>>
三星830 SSD的Smart值POR Recovery Count
查看>>
base642photo
查看>>
二分查找和数组合并
查看>>
【Java例题】5.5 两个字符串中最长公共子串
查看>>
python数据类型二
查看>>
Python-字典
查看>>
OS X下su和sudo
查看>>
乙级(Basic Level) 1011 个位数统计
查看>>
实验三+040+薛龚
查看>>
【错误】【vscode】输出中文是乱码问题
查看>>
Topological Sor-207. Course Schedule
查看>>
/MD, /MT, /LD (Use Run-Time Library)
查看>>
pahlcon:循环调度(Dispatch Loop)或跳转
查看>>
Java学习--异常处理及其应用类
查看>>
HTTP协议
查看>>
时间和时间戳的转换
查看>>
(七)磁盘调度
查看>>
[NOI 2011]阿狸的打字机
查看>>