Esiste un modo generico per ottenere la copia di un oggetto esistente in un altro?Copia oggetto in un altro
Si supponga che MyObj
contenga i campi id
e name
. Come questo:
MyObj myObj_1 = new MyObj(1, "Name 1");
MyObj myObj_2 = new MyObj(2, "Name 2");
Invece di
myObj_2.setName(myObj_1.getName()) // etc for each field
fare qualcosa come segue:
myObj_2.copyFrom(myObj_1)
in modo che essi sono diversi casi, ma hanno uguali proprietà.
vedere http://stackoverflow.com/questions/64036/how-do-you-make-a-deep-copy-of-an-object – karlipoppins
Questo [risposta] (http: // StackOverflow. it/questions/869033/how-do-i-copy-un-oggetto-in-java/9834683 # 9834683) ti aiuterà. –