对象拷贝---Spring框架中BeanUtils.copyProperties()与Object类中clone()

  1. 对象拷贝
    1.1. 浅拷贝
    拷贝对象时,只对基本数据类型进行了拷贝,而对引用数据类型只是进行引用的传递,而没有真实的创建一个新的对象,称为浅拷贝
    1.2. 深拷贝
    反之,在对引用数据类型进行拷贝的时候,创建了一个新的对象,并且复制其内的成员变量,称为深拷贝
  2. BeanUtils 里copyProperties(source,target)
    2.1 属性赋值是基于target 的成员列表,并且会跳过在source 中不存在的属性,不会因为两个对象之间的结构差异导致错误,但是必须保证同名的两个成员变量类型相同
    2.2 source, target对应的类中属性需有getter和setter方法,底层是根据get和set方法来赋值的
    2.3 copyProperties()是浅拷贝
  3. Object 类中的clone()默认是浅拷贝
    3.1 需要实现Cloneable 接口,重写clone()
public class User implements Cloneable {
    
    

    private Integer id;
    private String name;
    private int[] num;

    public User(Integer id, String name, int[] num) {
    
    
        this.id = id;
        this.name = name;
        this.num = num;
    }

    public int[] getNum() {
    
    
        return num;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
    
    
        return super.clone();
    }

    @Override
    public String toString() {
    
    
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", num=" + Arrays.toString(num) +
                '}';
    }
}
public class TestClone {
    
    

    public static void main(String[] args) {
    
    
        int[] arr = {
    
    1, 2, 3};
        User user1 = new User(1, "zs", arr);

        try {
    
    
            User user2 = (User) user1.clone();
            System.out.println(user1);    //User{id=1, name='zs', num=[1, 2, 3]}
            System.out.println(user2);    //User{id=1, name='zs', num=[1, 2, 3]}
            System.out.println(user1 == user2);   //false

            System.out.println(user1.getNum() == user2.getNum());  //true
            //以上说明,两个对象的引用类型属性(数组)是指向同一个,clone()是浅拷贝

        } catch (CloneNotSupportedException e) {
    
    
            e.printStackTrace();
        }
    }
}

3.2 实现深拷贝

public class User implements Cloneable {
    
    

    private Integer id;
    private String name;
    private int[] num;

    public User(Integer id, String name) {
    
    
        this.id = id;
        this.name = name;
    }

    public User(Integer id, String name, int[] num) {
    
    
        this.id = id;
        this.name = name;
        this.num = num;
    }

    public int[] getNum() {
    
    
        return num;
    }

    public void setNum(int[] num) {
    
    
        this.num = num;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
    
    
        User user = new User(this.id, this.name);
        int[] ints = new int[this.num.length];
        System.arraycopy(this.num, 0, ints, 0, this.num.length);
        user.setNum(ints);
        return user;
    }

    @Override
    public String toString() {
    
    
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", num=" + Arrays.toString(num) +
                '}';
    }
}
public class TestClone {
    
    

    public static void main(String[] args) {
    
    
        int[] arr = {
    
    1, 2, 3};
        User user1 = new User(1, "zs", arr);

        try {
    
    
            User user2 = (User) user1.clone();
            System.out.println(user1);    //User{id=1, name='zs', num=[1, 2, 3]}
            System.out.println(user2);    //User{id=1, name='zs', num=[1, 2, 3]}
            System.out.println(user1 == user2);   //false

            System.out.println(user1.getNum() == user2.getNum());  //false
            //此时,两个对象的引用类型属性(数组)不是指向同一个,实现深拷贝

        } catch (CloneNotSupportedException e) {
    
    
            e.printStackTrace();
        }
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_51681634/article/details/111147084