用Java克隆 [英] Cloning in Java

查看:96
本文介绍了用Java克隆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从互联网上读到了一段关于克隆的内容。但是我没理得,所以有人可以清楚地解释一下吗?

I read one paragraph from the Internet regarding cloning. But I didn't quite get it, so can someone explain it clearly?


如果班级有最后的字段,这些不能是给出克隆方法中的值。这会导致正确初始化对象的最终字段时出现问题。如果最后一个字段指的是对象的某个内部状态,那么克隆的对象最终会共享内部状态,这对于可变对象肯定是不正确的。

If the class has final fields, these can't be given a value in the clone method. This leads to problems with properly initializing the object's final fields. If the final field is referring to some internal state of the object, then the cloned object ends up sharing the internal state and this surely is not correct for mutable objects.

供参考,以下是链接:
http://www.jusfortechies.com/java/core-java/cloning.php

For reference, here is the link: http://www.jusfortechies.com/java/core-java/cloning.php

推荐答案

简而言之




  • o.clone()调用 Object.clone (),它生成 o 的内存副本。此后复制的引用不能更改为最终字段,因此我们有非自愿的别名;

  • 不需要的别名和可变性彼此不顺利:如果 o 更改,克隆也会不自觉地更改;

  • 您引用的博客文章是从这个复制的更好的博客文章,它通过良好的代码示例来证明这些陈述。

  • In short

    • o.clone() calls Object.clone(), which makes a memory copy of o. Thereafter the copied references cannot be changed for final field, so we have involuntary aliasing;
    • the unwanted aliasing and mutability do not go well with each other: If o changes, the clone involuntarily changes, too;
    • the statements in the blog post you cite are copied from this much better blog post, which attests the statements with good code examples.
    • 我相信这篇文章是通过clone-chaining讨论 clone()(通过 super.clone()),这样最终会调用 Object.clone(),这会产生一个平面内存副本通过本机代码克隆的对象。

      I believe the article is talking about clone() via clone-chaining (via super.clone()), such that eventually Object.clone() is called, which makes a flat memory copy of the object being cloned via native code.

      假设我们有以下示例(来自下面提到的博客文章):

      Let's say we have the following example (from the blog post mentioned below):

      public class Person implements Cloneable
      {
          private final Brain brain; // brain is final since I do not want 
                      // any transplant on it once created!
              // ...
      }
      

      person2 = (Person) person1.clone();
      

      然后person2与field1具有相同的字段大脑内存部分,即两者都拥有相同的参考到了同一个大脑。然后由于Person对象是可变的,他们可以学习东西:

      then person2 has the same memory-part for the field brain as person1, i.e. both hold the same reference to the same brain. Then since Person objects are mutable, they can learn things:

      person1.learn(dvorakTyping);
      

      然后神奇地说person2也可以在dvorak键盘上输入。对于不可变对象,这个问题不会发生(虽然克隆仍然有问题,因为最终字段仍然无法通过参数初始化,如在构造函数中)。

      then magically person2 can type on a dvorak keyboard as well. With immutable objects, this problem doesn't occur (though cloning's still problematic since final fields can still not be initialized via parameters, as in constructors).

      我上半句的原因是:你可以通过调用一个对象的构造函数来实现clone。有些人声称这是违反克隆合同的,但事实并非如此。这是一个好的博客文章,关于为什么要在克隆中调用构造函数(一个主要原因是那些最后的字段)。

      The reason for my very first half sentence: You can implement clone via calling one of the object's constructors. Some people claim this is against clone's contracts, but it isn't. Here's a good blog post about why to call a constructor within clone (one main reason being those final fields).

      阅读Hemal的评论mre的答案,我瞥了一眼博客文章引用的问题,事实证明,该帖子复制了一些句子来自我引用的博客文章,但没有非常好的代码示例。大声笑。

      Reading Hemal's comment on mre's answer, I did glance over the blog post the question cited, and it turns out, the post copied some sentences from the blog post I cited, but without the very good code examples. LOL.

      这篇关于用Java克隆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆