What is wrong with this clone()?

前端 未结 9 2016
梦如初夏
梦如初夏 2020-12-09 23:00

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

相关标签:
9条回答
  • 2020-12-09 23:08

    The standard pattern for making a class cloneable is:

    1. Implement Cloneable
    2. Override the clone() method and make it public
    3. In clone() call super.clone() and then copy any mutable object's state

    You should not create a new object using new. The proper way is to call super.clone() for a new instance. Object's clone() is special and will create a new copy of the object and copy its primitive fields and references.

    For example:

    public class Person implements Cloneable {
        protected String name;
        // Note that overridden clone is public
        public Object clone() {
            Person clone = (Person)super.clone();
            // No need to copy name as the reference will be
            // copied by Object's clone and String is immutable
            return clone;
        }
    }
    
    public class Employee extends Person {
        protected int id;
        protected java.awt.Point location;
        public Object clone() {
            Employee  clone = (Employee )super.clone();
            // No need to copy id as Object's clone has already copied it
            // Need to clone location as Point is mutable and could change
            clone.location = location.clone();
            return clone;
        }
    }
    
    0 讨论(0)
  • 2020-12-09 23:11

    Have you implemented the appropriate interface? It may be as simple as that, though you may have already accounted for this.

    See here for more information: http://java.sun.com/javase/6/docs/api/java/lang/Object.html#clone()

    0 讨论(0)
  • 2020-12-09 23:11

    I am not very familiar with Java but this may help: http://en.wikipedia.org/wiki/Clone_(Java_method)

    An Excerpt from the post:

    Another disadvantage is that one often cannot access the clone() method on an abstract type. Most interfaces and abstract classes in Java do not specify a public clone() method. As a result, often the only way to use the clone() method is if you know the actual class of an object; which is contrary to the abstraction principle of using the most generic type possible. For example, if one has a List reference in Java, one cannot invoke clone() on that reference because List specifies no public clone() method. Actual implementations of List like ArrayList and LinkedList all generally have clone() methods themselves, but it is inconvenient and bad abstraction to carry around the actual class type of an object.

    0 讨论(0)
  • 2020-12-09 23:12

    Did you implement the Cloneable interface on your object?

    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.

    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.

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

    Edit2: Rephrased my answer

    Edit3: 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.

    Edit5: Try this:

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

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

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

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

    Edit8: Fixed my sample, now it works without exception.

    (But the actual credit goes to Gábor Hargitai for super.clone())

    0 讨论(0)
  • 2020-12-09 23:18

    Mister Bloch of Effective Java has some interesting words to say on the use of clone.

    http://www.artima.com/intv/bloch13.html

    Generally the current thinking is to avoid the use of clone as it is error prone and greatly misunderstood, implementing copy constructor is an alternative strategy or using a brute force approach of deep copying the object using serialisation.

    0 讨论(0)
  • 2020-12-09 23:20

    Java's cloning mechanism is somewhat awkward. In order to be able to clone itself the class must do two things. First it must implement Clonable. Second it must override clone() and make it public.

    In your example, you overrode clone() but you are invoking clone() not on the Employee class but rather on the Object.class() where clone() is only protected.

    0 讨论(0)
提交回复
热议问题