Java – what’s wrong with this clone()?

I have written this cloning method when the parent class of the Employee class is abstract and the clone () method in the parent class is abstract I want to use this code to copy the original data type of the employee's object, instead of copying each original data and typing it separately, but this code has a problem with the line where I call the clone () method (this code is in the Employee class)

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

}

The error is: the method clone() of type object is not visible

However, my employee class is in the class hierarchy and can access the protected clone () method in the object class

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;
    }
}

Solution

Do you implement the clonable interface on the object?

However, I rarely use clone to copy objects An example of such security is array clone(). I prefer to use the copy constructor idiom or copy / assign manually

For background, there is item 11 in effective Java (version 2) Clonable interface is a special interface because it modifies the cloning behavior of object class Basically, it is a function of enabling class interface in Java

Edit: according to your example, you may need to wrap the clone () call in the try catch of clonenotsupportedexception in general

Edit2: restated my answer

Edit3: did you override clone () in the public context? In the example, you try to clone an object, which is located in Java Lang package – hardly the package where your code resides

Editor 4: I think the answer is already in other posts. I just want to reflect on the potential problems

Editor 5: try this:

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

Edit 6 and name your method public abstract object copy(). For example, in the implementation, use super Clone () – to avoid confusion

Editor 7 I did some masking and proposed the following solutions:

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();
    }
}

But it's basically the same concept as renaming an abstract method to clone ()

Edit8: fixed my sample and now it works without exception

(but the actual credit goes to G á bor hargitai's super. Clone ())

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>