这个clone()有什么问题? [英] What is wrong with this clone()?

查看:104
本文介绍了这个clone()有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了这个克隆方法,用于当Employee类的父级是抽象而父类中的clone()方法是abstract时。我想用这个代码复制Employee的对象的原始数据类型而不是复制每个原始数据类型单独,但此代码与我称为clone()方法的行有问题。 (此代码在Employee类中)

I have written this clone method for when the parent of the Employee class is abstract and the clone() method in the parent class is abstract.I wanted to copy the primitive data type of the Employee's object with this code instead of copying each primitive data type individually, but this code has problem with the line that I call clone() method. (This code is in Employee class)

public Object clone() {
    Object obj = new Object();
    Object object = obj.clone();  //Emphasis here
    return object;

}

错误是:方法clone()来自类型对象不可见。

the error is: The method clone() from the type Object is not visible.

但是我的Employee类在类层次结构中,可以访问Object类中的protected clone()方法。

But my Employee class is in the class hierarchy which can access the protected clone() method in the Object class.

这是我简单的Employee类:

This is my simple Employee class:

public class Employee extends Person implements Cloneable {
private int ID;

public Employee() {
    ID = 0;
}

public void setID(int ID) {
    this.ID = ID;
}

public int getID() {
    return ID;
}

public Object clone1() throws CloneNotSupportedException {
    try {
        Object obj = new Object();

        Object object = obj.clone();
        return object;
    } catch (CloneNotSupportedException ex) {
        return null;
    }
}


推荐答案

< strike>你在对象上实现了Cloneable接口吗?

但是,我很少会使用clone来复制对象。一个这样安全的例子是array.clone()。我宁愿使用copy-constructor习惯用法或手动复制/赋值。

However, there are very few cases I would use clone for copying an object. One such safe example is array.clone(). I would rather use the copy-constructor idiom or manually copying / assigning values around.

Effective Java (第2版)。可克隆接口是一种特殊的接口,因为它修改了有关克隆的 Object 类的行为。基本上它是一个在Java中启用 class 界面的功能。

There is Item#11 in Effective Java (2nd edition) about the background issue. Cloneable interface is a special kind of interface as it modifies the Object class' behavior regarding the cloning. Basically it is a feature enabling classinterface in Java.

编辑:根据您的示例,您可能需要在一般情况下,将clone()调用包装在CloneNotSupportedException的try-catch中。

Based on your example you might need to wrap the clone() call in a try-catch of CloneNotSupportedException in a general case.

编辑2:重述我的回答

编辑3:您是否覆盖了 public 上下文中的clone()?在示例中,您尝试克隆一个Object,它位于java.lang包中 - 几乎不是您的代码所在的包。

Did you override the clone() in the public context? In the sample you gave you try to clone an Object, which is in the java.lang package - hardly the package your code is in.

Edit4:我认为答案已经在其他帖子中,只是想反思潜在的问题。

I think the answer is already in the other posts, just wanted to reflect on the underlying issue.

编辑5:试试这个:

public Object clone1() throws CloneNotSupportedException {        
    return super.clone();        
}

Edit6 然后为您的方法命名 public abstract Object copy()例如,在实现中,使用super.clone() - 以避免混淆。

Edit6 Then name your method public abstract Object copy() for example and in the implementation, use the super.clone() - to avoid confusion.

编辑7 我做了一些日食,并推出了以下解决方案:

Edit7 I did some eclipsing and came out with the following solution:

public class Cloner {
    public static abstract class Person {
       protected abstract Object clone1() throws CloneNotSupportedException;
       public Object copy() throws CloneNotSupportedException {
           return clone1();
       }
    }
    public static class Employee extends Person implements Cloneable {
        @Override
        protected Object clone1() throws CloneNotSupportedException {
            return super.clone();
        }

    }
    public static void main(String[] args) throws Exception {
        new Employee().copy();
    }
}

但基本上它与重命名摘要的概念相同除了clone()之外的其他方法。

But basically it is the same concept as renaming your abstract method to something else than clone().

编辑8:修复我的样本,现在无异常。

Fixed my sample, now it works without exception.

(但实际功劳归于GáborHargitai super.clone()

这篇关于这个clone()有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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