简体   繁体   English

实现Clonable接口,但不必重写clone()方法

[英]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.

相关问题 当超类不可克隆时覆盖 Java 中的 Clone() 方法 - Overriding Clone() method in Java when superclass is not Clonable Java 中关于 clone() 、 Object 类和 Clonable Interface 的混淆 - confusion regarding clone() , Object class and Clonable Interface in Java 实现接口方法时不允许@Override - @Override is not allowed when implementing interface method 在实现Iterable接口时,iterator()方法不应该返回集合的克隆吗? - When implementing the Iterable interface, shouldn't the iterator() method return a clone of the collection? 在实现接口方法时我应该使用@Override标记吗? - Should I use @Override tag when implementing an interface method? 覆盖/更改实现接口 java 的最终类方法 - Override/Change final class method implementing interface java 当接口的方法没有任何实现而我们每次都必须重写它们时,使用接口的目的是什么? - What is the purpose of using interface when its methods don't have any implementation we have to override them every time? 为什么在从Interface实现方法时使用@Override? 它真的覆盖方法吗? - Why we use @Override while implementing a method from Interface ? Does it really Override the method? 方法与接口原型匹配,但不会覆盖 - Method matches interface prototype, but won't override 我需要能够克隆SOAPMessage对象,但是它没有实现Clonable。 我怎样才能做到这一点? - I need to be able to clone a SOAPMessage object, but it doesn't implement Clonable. How can I do this?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM