Java:Cloneable接口的基本原理 [英] Java: Rationale of the Cloneable interface
问题描述
为什么 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 aCopyable
, becauseCloneable
has problems. Besides the fact that it's misspelled,Cloneable
doesn't contain theclone
method. That means you can't test if something is an instance ofCloneable
, cast it toCloneable
, and invokeclone
. You have to use reflection again, which is awful. That is only one problem, but one I'd certainly solve.
这篇关于Java:Cloneable接口的基本原理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!