求教 关于Java1.8下 浅Clone与深Clone的区别

/**

  • */ package bean;

import java.math.BigDecimal;
import java.util.Date;

/**

  • @author hb
    *
    /
    public class JavaBean extends Bean implements Cloneable {
    /

    • 氏名 / private String Name; /
    • 年月日 / private Date birthday; /
    • アカウント / private String account; /
    • パスワード / private String password; /
    • 住所 */ private String[] address;

    /**

    • @return the name */ public String getName() { return Name; }

    /**

    • @param name
    • the name to set */ public void setName(String name) { Name = name; }

    /**

    • @return the birthday */ public Date getBirthday() { return birthday; }

    /**

    • @param birthday
    • the birthday to set */ public void setBirthday(Date birthday) { this.birthday = birthday; }

    /**

    • @return the account */ public String getAccount() { return account; }

    /**

    • @param account
    • the account to set */ public void setAccount(String account) { this.account = account; }

    /**

    • @return the password */ public String getPassword() { return password; }

    /**

    • @param password
    • the password to set */ public void setPassword(String password) { this.password = password; }

    /**

    • @return the address */ public String[] getAddress() { return address; }

    /**

    • @param address the address to set */ public void setAddress(String[] address) { this.address = address; }

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

    public static void main(String[] args) throws Exception {
    JavaBean bean = new JavaBean();
    bean.setSn(new BigDecimal(1));
    bean.setName("123");
    bean.setAddress(new String[]{"China","Shanghai","Pudong"});

    JavaBean cbean = (JavaBean) bean.clone();
    cbean.setSn(new BigDecimal(2));
    cbean.setName("456");
    cbean.setAddress(new String[]{"US","NewYork","NewYork"});
    
    System.out.println(bean);
    System.out.println(cbean);
    
    System.out.println(bean.getSn());
    System.out.println(cbean.getSn());
    
    System.out.println(bean.getName().hashCode()+":"+bean.getName());
    System.out.println(cbean.getName().hashCode()+":"+cbean.getName());
    
    System.out.println(bean.getAddress().hashCode()+":"+bean.getAddress()[0]);
    System.out.println(cbean.getAddress().hashCode()+":"+cbean.getAddress()[0]);
    

    }
    }

小弟最近在学习Java1.8下 关于浅Clone与深Clone的区别。发现有一些与以前不同的地方 感觉已经没有浅Clone这一说法 重载Clone方法后 对Clone后的对象方法赋值 已经不会改变原Clone对象 难道在Java1.8中取消了浅Clone?请各位大神指点

你这个验证做的有问题,基本类型深浅克隆都不会改变原对象的值,你可以在你的JavaBean里面再加一个对象的变量,然后验证下是否给这个对象的字段赋值会改变原对象里同样的值

浅克隆,依次复制一个对象的每个字段。
深克隆,如果一个对象的字段是引用的话,那么不是简单指向同一个对象,而是创建一个新的对象,然后再递归复制这个字段对象其中的每个字段。

好像是改了,我的也是

    // 測試克隆模式
    public static void main(String[] args) throws Exception {
        Sheep sheep = new Sheep("duoli", new Date(4554545454l));
        Sheep sheep2 = (Sheep) sheep.clone();

        System.out.println(sheep.getName());
        System.out.println(sheep.getBirthday().getTime());

        System.out.println(sheep2.getName());
        System.out.println(sheep2.getBirthday().getTime());

        System.out.println("-----------------------------");
        System.out.println("-----sheep.setBirthday(new Date(321321321l))");

        sheep.setBirthday(new Date(321321321l));

        System.out.println(sheep.getName());
        System.out.println(sheep.getBirthday().getTime());

        System.out.println(sheep2.getName());
        System.out.println(sheep2.getBirthday().getTime());

    }

输出结果:
duoli
4554545454
duoli

4554545454

-----sheep.setBirthday(new Date(321321321l))
duoli
321321321
duoli
4554545454