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 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误