Java cloneable_java浅克隆和深克隆

Java cloneable_java浅克隆和深克隆先来看看JDK的解释:java.lang.CloneableAclassimplementstheCloneableinterfacetoindicatetothejava.lang.Object.clone()methodthatitislegalforthatmethodtomakeafield-for-fieldcopyofinstances…


Jetbrains全系列IDE稳定放心使用

先来看看JDK的解释:

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.

By convention, classes that implement this interface should override Object.clone (which is protected) with a public method. See java.lang.Object.clone() for details on overriding this method.

Note that this interface does not contain the clone method. Therefore, it is not possible to clone an object merely by virtue of the fact that it implements this interface. Even if the clone method is invoked reflectively, there is no guarantee that it will succeed.

Since:

JDK1.0

Version:

1.17, 11/17/05

Author:

unascribed

See Also:

java.lang.CloneNotSupportedException

java.lang.Object.clone()

意思就是:

此类实现了 Cloneable 接口,以指示 Object.clone() 方法可以合法地对该类实例进行按字段复制。

如果在没有实现 Cloneable 接口的实例上调用 Object 的 clone 方法,则会导致抛出 CloneNotSupportedException 异常。

按照惯例,实现此接口的类应该使用公共方法重写 Object.clone(它是受保护的)。请参阅 Object.clone(),以获得有关重写此方法的详细信息。

注意,此接口不 包含 clone 方法。因此,因为某个对象实现了此接口就克隆它是不可能的。即使 clone 方法是反射性调用的,也无法保证它将获得成功。

我们平时需要对某个对象进行操作,但是这个对象被两个地方操作,却都要使用最原始时候的对象。这个时候就需要Cloneable接口出现了。并且如果我们对象里面的属性还有非基本类型对象的话也需要实现此接口直到没有属性是非基本类型对象为止(深拷贝)

这里有个不错的例子:http://cqitcs02370236.cnblogs.com/archive/2006/04/23/382644.html

2

顶

2

踩

分享到:

18e900b8666ce6f233d25ec02f95ee59.png

72dd548719f0ace4d5f9bca64e1d7715.png

2010-04-23 13:46

浏览 5671

评论

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/184076.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长

相关推荐

  • QStringList 的常规使用

    QStringList 的常规使用参考博客:https://blog.csdn.net/u013360881/article/details/52170487QStringList初始化QStringListqstrList;qstrList<<"Android"<<"QtCreator"<<"Java"<<"C++";

    2022年6月14日
  • 冰蝎下的反弹shell连接msfconsole

    冰蝎下的反弹shell连接msfconsole文章目录前言一、使用木马getshell1.搭建环境二、冰蝎配置三、kali监听总结前言好久没碰美少妇(MSF)了,恰巧昨天在群里水群,有个表哥问为什么msf监听不到数据。为此我带着表哥的疑问进行了简单的研究。大体的流程和思路我简单记录一下。其中的坑还是不少的,希望这篇文章对初识冰蝎的表哥们有点用处。一、使用木马getshell冰蝎之所以强还是在于他的动态二进制加密。这里呢,在冰蝎下载的包中给出了官方的webshell。在server文件夹下。这里呢,我踩过一个坑。不知道是我电脑配置的问题还是就

    2022年9月7日
  • [AV1] AV1技术专栏(正在更新中)

    [AV1] AV1技术专栏(正在更新中)这个专栏是我花了半年的时间阅读libav1,SVT-AV1以及dav1d的源码后摸索总结出来的AV1技能树,希望能帮助到你的AV1编解码器的学习。

    2022年9月23日
  • 对象字段java clone 中的浅复制和深复制

    对象字段java clone 中的浅复制和深复制

    2021年8月23日
  • navicat15手动激活码【2021.7最新】

    (navicat15手动激活码)好多小伙伴总是说激活码老是失效,太麻烦,关注/收藏全栈君太难教程,2021永久激活的方法等着你。IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.htmlMLZPB5EL5Q-eyJsaWNlbnNlSWQi…

    2022年3月21日
  • 背英语四级单词的三个小’窍门

    背英语四级单词的三个小’窍门

    2021年5月2日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号