简体   繁体   中英

How do I create a dereferenced clone of an object?

-The problem-

In a game (Model-View-Controller design) that I'm programming to practice with the (optimization?) of object creation, I want to create an object, and use that object to create clones. I do not want the clone's addresses to reference to the original object.

By what means can I create a series of clones that do not reference to the original object? (I believe the terminology is called "dereference".)

-In my code-

I have an EntityLibrary which instantiates and stores one instance of each kind of Entity child that I have (ie. Cat, Dog, Fish). Each individual Entity child in the EntityLibrary is meant to be retrieved by the Model class when the Model class decides that it needs a clone of that Entity child.

The problem that I'm running into is that the address of the Entity child's clone references to the same address of the original Entity child located in the EntityLibrary.

Try to use the Cloneable interface

http://coderevisited.com/cloneable-interface-in-java/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM