Java Object Clone

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.