Java的clone()菜鸟专区


clone()的存在更像是表达了一种模式:

内在的东西一处改变,便都改变了,外面的东西都可以定制,随心所欲,像极了我们生活的世界

user实体类代码如下:

package com.excel.upload.pojo;

/**实体类
 * @author xinjian
 * @create 2018/6/7 0007
 */
public class User implements Cloneable{

    private String name;

    private Integer age;
    //一处改变,都会改变的东西
    private Address address;

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

    public User getCloneObject(){
        try {
            return (User) this.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", address=" + address +
                '}';
    }
}

address实体类:

package com.excel.upload.pojo;

/**
 * @author xinjian
 * @create 2018/6/7 0007
 */
public class Address {

    private String name;

    private String code;

    private Integer peoples;

    public String getName() {
        return name;
    }

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

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public Integer getPeoples() {
        return peoples;
    }

    public void setPeoples(Integer peoples) {
        this.peoples = peoples;
    }

    @Override
    public String toString() {
        return "Address{" +
                "name='" + name + '\'' +
                ", code='" + code + '\'' +
                ", peoples=" + peoples +
                '}';
    }
}

测试代码:

    @Test
    public void testClone(){
        Address address = new Address();
        address.setCode("1001");
        address.setName("北京");
        address.setPeoples(20000000);

        User user = new User();
        user.setAddress(address);
        user.setAge(24);
        user.setName("jack");

        address.setPeoples(3565545);
        User user1 =  user.getCloneObject();
        user.setName("user-jack");
        System.out.println(user);
        System.out.println(user1);
        user1.getAddress().setPeoples(566666);
        user1.setName("jack-user1");
        System.out.println(user1);
    }

    @Test
    public void testMapClone(){
        Map map = new HashMap<>();
        map.put("xx","xx-value");
        map.put("yy","yy-value");
        map.put("zz","zz-value");

        Map map1 = (Map) ((HashMap) map).clone();
        map.remove("xx");
        System.out.println(map);
        map1.put("vv","sdsd");
        System.out.println(map1);
    }
运行结果:
{yy=yy-value, zz=zz-value}
{xx=xx-value, yy=yy-value, zz=zz-value, vv=sdsd}
User{name='user-jack', age=24, address=Address{name='北京', code='1001', peoples=3565545}}
User{name='jack', age=24, address=Address{name='北京', code='1001', peoples=3565545}}
User{name='jack-user1', age=24, address=Address{name='北京', code='1001', peoples=566666}}




你可能感兴趣的:(note)