Java:Cloneable接口的基本原理 [英] Java: Rationale of the Cloneable interface

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

问题描述

为什么 java.lang.Cloneable 界面中指定的 .clone()方法?

Why wasn't the .clone() method specified in the java.lang.Cloneable interface ?

推荐答案

基本上,它是一个破碎的界面。 Ken Arnold和Bill Venners在 Java设计问题中对此进行了讨论。

Basically, it's a broken interface. Ken Arnold and Bill Venners discussed it in Java Design Issues.

阿诺德:


如果我此时成为上帝,很多人可能都很高兴我是不,我会说弃用 Cloneable 并且有一个可复制,因为 Cloneable 有问题。除了拼写错误之外, Cloneable 不包含 clone 方法。这意味着您无法测试某些内容是否为 Cloneable 的实例,将其强制转换为 Cloneable ,并调用克隆。你必须再次使用反射,这很可怕。这只是一个问题,但我肯定会解决。

If I were to be God at this point, and many people are probably glad I am not, I would say deprecate Cloneable and have a Copyable, because Cloneable has problems. Besides the fact that it's misspelled, Cloneable doesn't contain the clone method. That means you can't test if something is an instance of Cloneable, cast it to Cloneable, and invoke clone. You have to use reflection again, which is awful. That is only one problem, but one I'd certainly solve.

这篇关于Java:Cloneable接口的基本原理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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