码迷,mamicode.com
首页 > 编程语言 > 详细

Java Object Clone

时间:2018-07-31 00:30:06      阅读:110      评论:0      收藏:0      [点我收藏+]

标签:ret   int   rop   sys   name   prope   use   注意   over   

Java Object Clone

        User user = new User();
        user.setName("tom");
        User user1 = new User();
        user1.setName("jerry");
        List<User> list = new ArrayList<>();
        list.add(user1);
        user.setChildren(list);
        user.setUser(user1);
        System.out.println(user);


        User2 user2 = new User2();
        BeanUtils.copyProperties(user2,user);
        System.out.println(user2);
        List<User2> children = user2.getChildren();
        // 此处会出现错误
        for (User2 user21 :children  ) {
            System.out.println(user21);
        }
        User user3 = (User) BeanUtils.cloneBean(user);
        System.out.println(user3);

user:

package com.test;

import java.util.List;

public class User {
    private String name;
    private List<User> children;
    private User user;

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @Override
    public String toString() {
        return "User{" +
                "name=‘" + name + ‘\‘‘ +
                ", children=" + children +
                ", user=" + user +
                ‘}‘;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<User> getChildren() {
        return children;
    }

    public void setChildren(List<User> children) {
        this.children = children;
    }
}

user2:

package com.test;

import java.util.List;

public class User2 {
    private String name;
    private List<User2> children;

    private User2 user2;

    public User2 getUser2() {
        return user2;
    }

    public void setUser2(User2 user2) {
        this.user2 = user2;
    }

    @Override
    public String toString() {
        return "User2{" +
                "name=‘" + name + ‘\‘‘ +
                ", children=" + children +
                ", user2=" + user2 +
                ‘}‘;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<User2> getChildren() {
        return children;
    }

    public void setChildren(List<User2> children) {
        this.children = children;
    }
}

 

注意和spring beanutils的区别:

https://blog.csdn.net/langqiao123/article/details/72961383/

传说spring Beanutils的性能要更好一些;

 

Java Object Clone

标签:ret   int   rop   sys   name   prope   use   注意   over   

原文地址:https://www.cnblogs.com/stono/p/9393488.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!