没有实现可克隆接口的对象克隆 [英] object cloning with out implementing cloneable interface

查看:89
本文介绍了没有实现可克隆接口的对象克隆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

克隆对象我需要实现'cloneable'接口。因为这里我的类是一个jar文件(我的意思是API)。所以我无法编辑课程。我听说所有类都扩展了基础对象类,这个对象类实现了可克隆的接口。这是否意味着我们可以直接克隆对象而不实现接口。如果是这样,在我的日食中,我没有任何克隆对象的选项。 是否有其他方法可以在不实现可克隆接口的情况下克隆对象。请解释。

to clone the object do i need to implement 'cloneable' interface. because here my class is a jar file(i mean API). so i can't edit the class. i heard that all classes are extends the base object class and this object class implements cloneable interface. does that mean can we directly clone the object with out implementing the interface. if so in my eclipse i am not getting any option to clone the object. is there any other way to clone the object without implementing the cloneable interface. please explain.

推荐答案

Java Object 类没有实现 Cloneable 界面。但它有 clone()方法。但是这个方法是 protected 并且如果在没有实现 CloneNotSupportedException >可克隆的界面。因此,如果您无法修改要克隆的类,那么您将失去运气,并且必须找到另一种方法来复制实例。

The Java Object class does not implements the Cloneable interface. It does however have the clone() method. But this method is protected and will throw CloneNotSupportedException if called on an object that does not implement the Cloneable interface. So if you cannot modify the class you want to clone you're out of luck and will have to find another way to copy the instance.

但是应该注意到Java中的克隆系统充满漏洞,通常不再使用。从2002年开始,与Josh Bloch一起查看访谈,解释一些问题。

It should be note however that the clone system in Java is full of holes and generally not used anymore. Check out this interview with Josh Bloch from 2002 explaining a few of the issues.

这篇关于没有实现可克隆接口的对象克隆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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