Ronan Posted February 27, 2010 Share Posted February 27, 2010 Hey there, Well - getting forward with my game, but I'm finding I'm in need of 'cloning' objects, as such; reusing all the data from one object in another one, in a similar fashion to the 'ByVal' implementations in other languages. However, I've never looked into this in Java before. With a bit of searching around, it seems that implementing your own clone method is the most viable solution, but I'm wondering if anyone else has more experience in this area and some potential solutions. :) Link to comment Share on other sites More sharing options...
dsavi Posted March 1, 2010 Share Posted March 1, 2010 I would say that using your own clone() method is probably a good idea. Just make so clone() makes so that that the correct properties are made unique or reset so that you can differentiate between objects. Link to comment Share on other sites More sharing options...
obfuscator Posted March 1, 2010 Share Posted March 1, 2010 Hmm, I'm not sure exactly what you're trying to do (never used byVal) but couldn't you just do something like this? Object a (property a, property b, property c); //object you want to clone to clone: Object b(a.getProperty(a), a.getProperty(b), a.getProperty(c)); Unless I'm not understanding what you want to do that seems like an easy way to do it.... If you wanted a unique object ID or something you could do a(1, b, c); b(1+a.getProperty(id); "It's not a rest for me, it's a rest for the weights." - Dom Mazzetti Link to comment Share on other sites More sharing options...
dsavi Posted March 1, 2010 Share Posted March 1, 2010 That seems like a pretty roundabout way of doing it IMO... clone methods are easy to write and make for more readable code. Link to comment Share on other sites More sharing options...
obfuscator Posted March 1, 2010 Share Posted March 1, 2010 That seems like a pretty roundabout way of doing it IMO... clone methods are easy to write and make for more readable code.Wouldn't a clone method basically just contain the same thing? "It's not a rest for me, it's a rest for the weights." - Dom Mazzetti Link to comment Share on other sites More sharing options...
dsavi Posted March 1, 2010 Share Posted March 1, 2010 Yes, but it enables you to write the code that resets the properties only once- and invoke it in one line of code. Also, nesting functions isn't nearly as readable as just one function call. Link to comment Share on other sites More sharing options...
obfuscator Posted March 1, 2010 Share Posted March 1, 2010 Quite true dear sir. I shall now attempt to write such a method being extremely bored at work.. EDIT: Never mind, I won't, I forgot I don't have Java :( "It's not a rest for me, it's a rest for the weights." - Dom Mazzetti Link to comment Share on other sites More sharing options...
Ronan Posted March 1, 2010 Author Share Posted March 1, 2010 Thanks for the replies! :) I did indeed end up just writing my own clone methods, they're surprisingly easy with most classes actually, certainly more so than I had imagined. I was rather surprised that there was no 'built-in' way to easily get another 'clone' of an object in java without writing your own method actually, but then I suppose that it's rarely needed, so writing a few methods yourself is hardly any trouble. If interested, currently the area that it's being utilised in is within levels of my game. Once all the levels have been completed, large modifications have been made to the objects that the level is comprised of so resetting the level involves changing all the objects back to their original state. Doing so via cloning is a relatively easy solution. Whenever a new level is created, a clone is placed in an ArrayList which contains all levels in their 'initial' state, allowing me to replace a modified level with a clone of their initial-state counterpart whenever it should be played again. Another option was to define a structure which would contain all the objects at their initial state of the level and create a constructor for a level based on the structure - again, would involve cloning too. Link to comment Share on other sites More sharing options...
Jard_Y_Dooku Posted April 2, 2010 Share Posted April 2, 2010 Don't forget to implement the Cloneable interface for classes that have a clone() method. Never trust anyone. You are always alone, and betrayal is inevitable.Nothing is safe from the jaws of the decompiler. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now