在LinkedList上实现克隆 [英] Implementing clone on a LinkedList

查看:190
本文介绍了在LinkedList上实现克隆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 DoubleLinkedList 上实现 clone()方法。现在,问题是,与仅创建一个新的 DoubleLinkedList 并用我当前的DoubleLinkedList的所有元素填充一样,通过约定实现它要麻烦得多。 / p>

这样做时有什么不便之处吗?



这是我目前的做法:

  @覆盖
public DoubleLinkedList< T> clone(){
DoubleLinkedList< T> dll =新的DoubleLinkedList< T>();

为(T元素:dll){
dll.add(element);
}

返回dll;
}

这是惯例:

  @Override 
public DoubleLinkedList< T> clone(){
try {
DoubleLinkedList< T> dll =(DoubleLinkedList< T>)super.clone();
//要复制元素的复杂代码
返回dll;
} catch(CloneNotSupportedException e){
throw new InternalError(e.toString());
}
}


解决方案

As您正确地指出,惯例是始终在 clone() super.clone() c>。从 <$上的API文档c $ c> Object#clone()


按照惯例,返回的对象应通过调用super.clone获得。如果一个类及其所有超类(对象除外)都遵守此约定,则x.clone()。getClass()== x.getClass()就是这种情况。


您的首次尝试(不使用 super.clone())以下问题:



假设我有

 类IntDoubleLinkedList扩展了DoubleLinkedList<整数>实现Cloneable 

(而 IntDoubleLinkedList 不麻烦重写 clone()),然后运行以下代码:

  IntDoubleLinkedList idll =新的IntDoubleLinkedList(); 
IntDoubleLinkedList idll2 =(IntDoubleLinkedList)idll.clone();

会发生什么? 您的 DoubleLinkedList 的克隆方法将被执行,如果不通过super.clone(),它将返回 DoubleLinkedList ,这又不能转换为 IntDoubleLinkedList 会抛出 ClassCastException



怎么办super.clone()解决此问题?好吧,如果每个人都遵循在重写克隆方法中调用 super.clone()的约定,那么 Object.clone()最终将被调用,并且此实现将创建适当类型的实例(在这种情况下为 IntDoubleLinkedList )!


I am trying to implement a clone() method on a DoubleLinkedList. Now, the problem is that implementing it by "the convention" is a lot more troublesome than just creating a new DoubleLinkedList and filling it with all the elements of my current DoubleLinkedList.

Is there any inconvenient I am not seeing when doing that?

Here is my current approach:

@Override
public DoubleLinkedList<T> clone() {
    DoubleLinkedList<T> dll = new DoubleLinkedList<T>();

    for (T element : dll) {
        dll.add(element);
    }

    return dll;
}

Here is what it would be by the convention:

@Override
public DoubleLinkedList<T> clone() {
    try {
        DoubleLinkedList<T> dll = (DoubleLinkedList<T>)super.clone();
        //kinda complex code to copy elements
        return dll;
    } catch (CloneNotSupportedException e) {
        throw new InternalError(e.toString());
    }
}

解决方案

As you correctly point out, the convention is to always call super.clone() in the beginning of an implementation of clone(). From the API docs on Object#clone():

By convention, the returned object should be obtained by calling super.clone. If a class and all of its superclasses (except Object) obey this convention, it will be the case that x.clone().getClass() == x.getClass().

Your first attempt (without using super.clone()) has the following problem:

Suppose I have

class IntDoubleLinkedList extends DoubleLinkedList<Integer> implements Cloneable

(and that IntDoubleLinkedList does not bother to override clone()) and I run the following code:

IntDoubleLinkedList idll = new IntDoubleLinkedList();
IntDoubleLinkedList idll2 = (IntDoubleLinkedList) idll.clone();

What will happen? The clone method of your DoubleLinkedList will be executed, which, if it doesn't go through super.clone(), returns an instance of DoubleLinkedList which in turn can not be casted to an IntDoubleLinkedList. A ClassCastException will be thrown!

So how does super.clone() solve this issue? Well, if everybody stick to the convention of calling super.clone() in an overriden clone method, Object.clone() will eventually be called, and this implementation will create an instance of a proper type (IntDoubleLinkedList in this case)!

这篇关于在LinkedList上实现克隆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆