如何正确覆盖克隆方法? [英] How to properly override clone method?

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

问题描述

我需要在没有超类的对象之一中实现深度克隆.

I need to implement a deep clone in one of my objects which has no superclass.

处理超类(即Object)抛出的已检查CloneNotSupportedException的最佳方法是什么?

What is the best way to handle the checked CloneNotSupportedException thrown by the superclass (which is Object)?

一位同事建议我按以下方式处理:

A coworker advised me to handle it the following way:

@Override
public MyObject clone()
{
    MyObject foo;
    try
    {
        foo = (MyObject) super.clone();
    }
    catch (CloneNotSupportedException e)
    {
        throw new Error();
    }

    // Deep clone member fields here

    return foo;
}

这对我来说似乎是一个很好的解决方案,但我想把它扔给 StackOverflow 社区,看看是否还有我可以包含的任何其他见解.谢谢!

This seems like a good solution to me, but I wanted to throw it out to the StackOverflow community to see if there are any other insights I can include. Thanks!

推荐答案

你一定要使用 clone 吗?大多数人都同意 Java 的 clone 已损坏.

Do you absolutely have to use clone? Most people agree that Java's clone is broken.

Josh Bloch 谈设计 - 复制构造函数与克隆

如果你读过我书中关于克隆的文章,尤其是在字里行间,你就会知道我认为 clone 已经被严重破坏了.[...] 遗憾的是 Cloneable 被破坏了,但它发生了.

If you've read the item about cloning in my book, especially if you read between the lines, you will know that I think clone is deeply broken. [...] It's a shame that Cloneable is broken, but it happens.

您可以在他的著作Effective Java 2nd Edition, Item 11: Override clone judiciously 中阅读有关该主题的更多讨论.他建议改用复制构造函数或复制工厂.

You may read more discussion on the topic in his book Effective Java 2nd Edition, Item 11: Override clone judiciously. He recommends instead to use a copy constructor or copy factory.

他接着写了几页关于如何实现clone的文章,如果你觉得必须的话.但他以这个结束:

He went on to write pages of pages on how, if you feel you must, you should implement clone. But he closed with this:

所有这些复杂性真的有必要吗?很少.如果你扩展一个实现了Cloneable 的类,你别无选择,只能实现一个行为良好的clone 方法.否则,您最好提供对象复制的替代方法,或者干脆不提供该功能.

Is all this complexities really necessary? Rarely. If you extend a class that implements Cloneable, you have little choice but to implement a well-behaved clone method. Otherwise, you are better off providing alternative means of object copying, or simply not providing the capability.

重点是他的,不是我的.

The emphasis was his, not mine.

既然您明确表示除了实现 clone 之外别无选择,那么在这种情况下您可以执行以下操作:确保 MyObject 扩展 java.lang.Object 实现 java.lang.Object.lang.Cloneable.如果是这种情况,那么您可以保证永远不会捕获CloneNotSupportedException.像某些人建议的那样抛出 AssertionError 似乎是合理的,但您也可以添加注释来解释为什么在这种特殊情况下永远不会输入 catch 块.

Since you made it clear that you have little choice but to implement clone, here's what you can do in this case: make sure that MyObject extends java.lang.Object implements java.lang.Cloneable. If that's the case, then you can guarantee that you will NEVER catch a CloneNotSupportedException. Throwing AssertionError as some have suggested seems reasonable, but you can also add a comment that explains why the catch block will never be entered in this particular case.

或者,正如其他人所建议的那样,您或许可以在不调用 super.clone 的情况下实现 clone.

Alternatively, as others have also suggested, you can perhaps implement clone without calling super.clone.

这篇关于如何正确覆盖克隆方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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