[英]Implementing the Clonable interface, but don't have to override the clone() method
I'm learning Java and there is something that I couldn't understand. 我正在学习Java,有些东西我听不懂。 If I understand correctly, by implementing an interface I am forced to implement all the methods that the interface has.
如果我正确理解,则通过实现接口,我将被强制实现接口具有的所有方法。
But, what in the case of the Clonable ? 但是,在“ 克隆人”的情况下呢? If I implement it, I'm not forced to implement the clone method.
如果实现了它,则不必强制实现clone方法。 Why is this happening?
为什么会这样呢?
Clonable
is a marker interface, which let you know that object can be cloned. Clonable
是标记接口,可让您知道可以克隆对象。 You are not forced to implement clone
method, because this method already exists in your class with protected
access modifier (inherit from Object::clone). 您不必强制实现
clone
方法,因为该方法已经存在于您的类中并带有protected
访问修饰符(从Object :: clone继承)。
因为Cloneable接口没有这种方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.