Java Object Clone
User User =NewUser (); User.setname ("Tom"); User User1=NewUser (); User1.setname ("Jerry"); List<User> list =NewArraylist<>(); List.add (user1); User.setchildren (list); User.setuser (user1); SYSTEM.OUT.PRINTLN (user); User2 User2=NewUser2 (); Beanutils.copyproperties (User2,user); System.out.println (User2); List<User2> children =User2.getchildren (); //An error will appear here for(User2 user21:children) {System.out.println (USER21); } User User3=(user) Beanutils.clonebean (user); System.out.println (USER3);
User
Packagecom.test;Importjava.util.List; Public classUser {PrivateString name; PrivateList<user>children; Privateuser User; PublicUser GetUser () {returnuser; } Public voidsetUser (user user) { This. user =user; } @Override PublicString toString () {return"user{" + "name=" + name + ' \ ' + ", children=" + children + ", user=" + use R + '} '; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PublicList<user>GetChildren () {returnchildren; } Public voidSetchildren (list<user>children) { This. Children =children; }}
User2:
Packagecom.test;Importjava.util.List; Public classUser2 {PrivateString name; PrivateList<user2>children; PrivateUser2 User2; PublicUser2 GetUser2 () {returnUser2; } Public voidSetUser2 (User2 user2) { This. User2 =User2; } @Override PublicString toString () {return"user2{" + "name=" + name + ' \ ' + ", children=" + children + ", user2=" + u Ser2 + '} '; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PublicList<user2>GetChildren () {returnchildren; } Public voidSetchildren (list<user2>children) { This. Children =children; }}
Note the difference from Spring beanutils:
72961383/
Legend has it that spring beanutils has better performance;
Java Object Clone