为什么 Cloneable 不被弃用? [英] Why is Cloneable not deprecated?

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

问题描述

众所周知,Java中的Cloneable接口被破坏了.造成这种情况的原因有很多,我就不说了;其他人已经做到了.这也是 Java 架构师自己的立场.

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 中它是仍然没有被弃用)?

My question is therefore: why has is not been deprecated yet? If the core Java team have decided that it is broken, then they must also have considered deprecation. What are their reasons against doing so (in Java 8 it is still not deprecated)?

推荐答案

有一个bug 1997 年提交给 Java Bug 数据库 关于添加 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:

Sun 的技术审查委员会 (TRC) 详细考虑了这个问题并建议不要采取除改进当前 Cloneable 界面的文档.这是完整的建议正文:

Sun's Technical Review Committee (TRC) considered this issue at length and recommended against taking any action other than improving the documentation of the current Cloneable interface. Here is the full text of the recommendation:

现有的 Java 对象克隆 API 存在问题.有一个java.lang.Object 上受保护的克隆"方法,并且有一个接口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 添加克隆"方法将是一个不相容的变化.它不会破坏二进制兼容性,但会破坏源兼容性.轶事证据表明,在实践中有许多情况下类支持可克隆接口但未能提供公共克隆方法.后讨论中,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 以反映最初的预期目的的可克隆.TRC 以 5 比 2 的多数反对这种做法.主要的担忧是这会增加更多的混乱(包括拼写混乱!)到已经混乱的图片.

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 接口的文档,以更好地描述它的用途和描述最佳实践"实施者.

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 设为弃用"的原因是技术审查委员会决定修改现有文档就足够了使这个界面有用.他们也这样做了.在 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 接口来指示Object.clone() 方法,该方法制作一个该类的实例的字段对字段副本.

A class implements the Cloneable interface to indicate to the Object.clone() method that it is legal for that method to make a field-for-field copy of instances of that class.

尝试克隆未实现 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:

一个类实现了 Cloneable 接口来指示Object.clone() 方法,该方法制作一个该类的实例的字段对字段副本.调用对象的未实现 Cloneable 的实例上的 clone 方法接口导致异常 CloneNotSupportedException 被抛出.

A class implements the Cloneable interface to indicate to the 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.

按照惯例,实现这个接口的类应该覆盖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.

注意这个接口不包含clone方法.所以,仅凭事实不可能克隆一个对象它实现了这个接口.即使调用了 clone 方法反思,不能保证它会成功.

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 不被弃用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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