如果clone()
是 Object
的一部分class ,那么为什么我们需要实现 Clonable
使用界面clone()
?
我读过clone()
是 Object
的 protected 成员,那么clone()
之间有什么关系呢?和Clonable
界面。
抱歉,如果我听起来很愚蠢。我刚刚开始学习Java。
最佳答案
Cloneable
是一个标记接口(interface)。它没有任何方法。只是将您的类列入白名单以使其可克隆
来自docs
A class implements the Cloneable interface to indicate to the Object.clone() method that it is legal for that method to make a field-for-field copy of instances of that class. Invoking Object's clone method on an instance that does not implement the Cloneable interface results in the exception CloneNotSupportedException being thrown.
关于java - 关于 Java 中的 clone() 、对象类和 Cloneable 接口(interface)的混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59628805/