Gueis 2016-08-04 00:29 采纳率: 0%
浏览 1513

求教 关于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?请各位大神指点

  • 写回答

3条回答 默认 最新

  • 暮云收尽溢清寒 2016-08-04 01:32
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的
  • ¥15 全志H618ROM新增分区
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符