本文介绍了为什么Cloneable不被弃用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常可以理解,Java中的 Cloneable 接口已被破坏。这有很多原因,我不会提及; 已经做到了。它也是本身的职位。

It is commonly understood that Cloneable interface in Java is broken. There are many reasons for this, which I will not mention; others already did it. It is also the position of Java architects themselves.

因此,我的问题是:为什么还没有被弃用?如果核心Java团队已经确定它已被破坏,那么他们也必须考虑弃用。他们反对这样做的原因是什么(在Java 8中它是于1997年提交给,内容是关于添加 clone()方法到 Cloneable ,所以它将不再是无用的。它以不会修复的决议结束,理由如下:

There is a bug submitted in 1997 to Java Bug Database about adding clone() method to Cloneable, so it would no longer be useless. It was closed with resolution "won't fix" and justification was as follows:

现有的Java对象克隆API存在问题。 java.lang.Object上有一个
保护的clone方法,并且有一个接口
java.lang.Cloneable。目的是如果一个类想要允许
其他人克隆它,那么它应该支持Cloneable
接口并使用
公共克隆方法覆盖默认的受保护克隆方法。不幸的是,由于原因很容易在
时间迷失中,Cloneable接口没有定义克隆
方法。

The existing Java object cloning APIs are problematic. There is a protected "clone" method on java.lang.Object and there is an interface java.lang.Cloneable. The intention is that if a class wants to allow other people to clone it, then it should support the Cloneable interface and override the default protected clone method with a public clone method. Unfortunately, for reasons conveniently lost in the mists of time, the Cloneable interface does not define a clone method.

这种组合导致了相当混乱。有些类
声称支持Cloneable,但不小心忘记支持
克隆方法。开发人员对Cloneable如何工作以及克隆应该做什么感到困惑。

This combination results in a fair amount of confusion. Some classes claim to support Cloneable, but accidentally forget to support the clone method. Developers are confused about how Cloneable is supposed to work and what clone is supposed to do.

不幸的是,向Cloneable添加克隆方法将是一个
不兼容的变化。它不会破坏二进制兼容性,但它会破坏源兼容性
。轶事证据表明,在
实践中,有许多类支持
Cloneable接口,但未能提供公共克隆方法。在
讨论之后,TRC一致建议我们不要修改
现有的Cloneable接口,因为兼容性影响。

Unfortunately, adding a "clone" method to Cloneable would be an incompatible change. It won't break binary compatibility, but it will break source compatibility. Anecdotal evidence suggests that in practice there are a number of cases where classes support the Cloneable interface but fail to provide a public clone method. After discussion, TRC unanimously recommended that we should NOT modify the existing Cloneable interface, because of the compatibility impact.

另一个提议是添加一个新接口
java.lang.PubliclyCloneable以反映Cloneable的原始预期用途
。以5比2多数,TRC建议不要这样做。
主要担心的是,这会给已经困惑的图片增加更多的混淆(包括
拼写错误!)。

An alternative proposal was to add a new interface java.lang.PubliclyCloneable to reflect the original intended purpose of Cloneable. By a 5 to 2 majority, TRC recommended against this. The main concern was that this would add yet more confusion (including spelling confusion!) to an already confused picture.

TRC一致建议我们应该在现有Cloneable接口中添加额外的
文档,以更好地描述
如何使用它,并描述
实现者的最佳实践。 / p>

TRC unanimously recommended that we should add additional documentation to the existing Cloneable interface to better describe how it is intended to be used and to describe "best practices" for implementors.

因此,虽然这不是直接关于已弃用的,但不使Cloneable弃用的原因是技术评论Comitee决定修改现有文档足够使这个界面变得有用。所以他们做到了。在Java 1.4之前, Cloneable 记录如下:

So, although this is not directly about deprecated, the reason for not making Cloneable "deprecated" is that Technical Review Comitee decided that modifying existing documentation will be sufficient enough to make this interface useful. And so they did. Until Java 1.4, Cloneable was documented as follows:

尝试克隆未实现Cloneable
接口的实例导致异常CloneNotSupportedException抛出

Attempts to clone instances that do not implement the Cloneable interface result in the exception CloneNotSupportedException being thrown.

接口Cloneable声明没有方法。

The interface Cloneable declares no methods.

自Java 1.4(其中)在2002年2月发布了截至当前版本(Java 8),它看起来像这样:

Since Java 1.4 (which was released in February 2002) up to current edition (Java 8) it looks like this:

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

By convention, classes that implement this interface should override Object.clone (which is protected) with a public method. See 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.

这篇关于为什么Cloneable不被弃用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-15 03:01