本文介绍了为什么#clone()不在Cloneable界面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在阅读正确执行数组的深层拷贝,但是我对如何实现 c> Object< T> 并强制 Java Universe 中的每个类都可以对其进行参数化,而且这只是为了使这个半被淘汰的机制工作好一点点?还要记住,这段代码是完全合法的:
Bear in mind that Generics would be of little use for cloning: imagine protected T clone() in Object: where would T come from? Would we need Object<T> and force each and every class in Java universe to be parameterized on itself, and all this just to make this semi-deprecated mechanism work a tiny bit better? Keep also in mind that this code is perfectly legal:
public class TheMightyOne implements Cloneable { @Override public TheMightyOne clone() { return (TheMightyOne) super.clone(); } }
您可以调用它:
TheMightyOne one = new TheMightyOne(); TheMightyOne two = one.clone(); // do downcasts needed
这篇关于为什么#clone()不在Cloneable界面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!