开发者

Why isn't there a easy way to copy objects in Java?

I have a cla开发者_StackOverflow中文版ss TimeLine in my GUI. I have a function where I would like to copy this TimeLine and modify the data in it whithout the TImeLine in my GUI won't be affected.

I've searched some forums and haven't found an easy way because Java let's the references stick. Why isn't there a easy way to create a new Object (TimeLine) that hasn't the reference to the previous one?

Please help my make a copy of this TimeLine object!


The clone() method and Cloneable interface, suggested by other authors here, were created with the incorrect assumption that it would be a good idea to have a generic copying method. The default implementations does a shallow clone of the current object, but you could override it to do a deep clone.

There is no correct, generic way to copy arbitrary objects, what you want to copy depends on the objects involved. For example, immutable objects never need copying (that'd just be a waste of space), while some types of objects can't be copied (how would you copy a FileOutputStream, for example?).

The way I find most elegant is immutable objects with methods that return a copy with just one field changed:

class Pony {
    private final String name;
    private final Color color;
    private final int tailLength;

    // constructors and accessors omitted

    Pony withName(String newName) {
        return new Pony(newName, color, tailLength);
    }

    Pony withColor(Color newColor) {
        return new Pony(name, newColor, tailLength);
    }

    Pony withTailLength(String newTailLength) {
        return new Pony(name, color, newTailLength);
    }
}

// Usage:

Pony tony = new Pony("Tony", Color.DAPPLE, 32);
Pony maurice = tony.withName("Maurice") // Maurice is like Tony, but black.
                   .withColor(Color.BLACK);

Unfortunately, you get a lot of boilerplate this way, and there's no mainstream IDE support either (there may be plugins, though). Related to this is the Builder pattern recommended by Josh Bloch in Effective Java.


Instead of clone() you might want to consider writing a copy constructor for your class:

public TimeLine(TimeLine original) {
    this.foo = original.foo;
    this.bar = original.bar;
}

Be careful when copying the value of any reference fields in your class. Be sure whether you want a shallow copy or a deep copy.


In Java using the operator "=" your are simply copying references to objects. If you want to implement copy by value use clone operation (clone() method of the class Object) or implement your own clone method (overriding the implemented one).

Pay attention that if your class stores other object inside it, these objects should eventually be cloned too (DEEP COPY).


if you want to copy an object use clone()

if I misunderstood your question please comment


You're looking for the clone method. (I've gone back and forth as to which documentation to cite, that is Wikipedia, it's treatment is more thorough. The Java doc's are more official, of course).

Eg.

MyObject a = new MyObject();
a.setSomething( 2 );
MyObject b = a.clone();

// now b.getSomething().equals(a.getSomething()) (maybe == too, depends on class)
// and b.equals(a)
// b != a
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜