java中可克隆接口的用途是什么? [英] What is the use of cloneable interface in java?
问题描述
实现可克隆接口有什么用,因为它是一个标记接口?
What is the use of implementing a cloneable interface as it is a marker interface?
我总是可以在我的类中创建一个公共Object clone()方法。克隆接口的实际目的是什么?
I can always make a public Object clone() method in my class. What is the actual purpose of cloneable interface?
推荐答案
那是因为 clone()
方法抛出 CloneNotSupportedException
如果你的对象不是 Cloneable
。
That's because the clone()
method throws CloneNotSupportedException
if your object is not Cloneable
.
你应该看一下 clone()
方法的文档。
You should take a look at the documentation for clone()
method.
以下是如何 clone()
方法在类中声明对象
:
Following is how clone()
method is declared in the class Object
:
protected Object clone() throws CloneNotSupportedException
注意:
此外,已经意识到克隆
已损坏。这里的这个答案解释了为什么以及如何避免使用它。
Also, it's been realized that Clone
is broken. This answer here in SO explains why and how you can avoid using it.
这篇关于java中可克隆接口的用途是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!