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

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

问题描述

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

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

处理选中的的最佳方法是什么? CloneNotSupportedException 由超类抛出( Object )?

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的克隆被破坏。

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

Josh Bloch on Design - 复制构造函数与克隆


如果你已经在我的书中阅读了关于克隆的项目,特别是如果你在各行之间阅读,你会知道我认为 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 明智地中阅读有关该主题的更多讨论。他建议改为使用复制构造函数或复制工厂。

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.

他继续写了一些页面,如果你认为必须如何,你应该实现克隆。但他以此结束了:

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.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.

另外,正如其他人也建议的那样,你可以实现 clone 不调用 super.clone

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

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

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