简体   繁体   English

为什么我们调用Object.clone()方法?

[英]Why we invoke Object.clone() method?

This is an implementation of the SinglyLinkedList.clone method given in my book. 这是我书中给出的SinglyLinkedList.clone方法的实现。

public SinglyLinkedList<E> clone(){
    SinglyLinkedList<E> other =(SinglyLinkedList<E>) super.clone();
     // rest of the implementation. 
}

Why we invoked super.clone() . 为什么我们调用super.clone()

From the Java API doc of Object.clone(): “this method creates a new instance of the class of this object and initializes all its fields with exactly the contents of the corresponding fields of this object, as if by assignment”. 从Object.clone()的Java API文档:“此方法创建此对象的类的新实例,并使用此对象的相应字段的内容初始化其所有字段,就像通过赋值一样”。 So I guess the purpose is to create the SinglyLinkedList object that will be the clone. 所以我想我的目的是创建将成为克隆的SinglyLinkedList对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM