当前位置: 首页 > news >正文

凡科网免费做网站怎么用程序做网站

凡科网免费做网站,怎么用程序做网站,手机网页视频提取工具,wordpress 新增选项关于Clone 一般情况下,如果使用clone()方法,则需满足以下条件。 1、对任何对象o,都有o.clone() ! o。换言之,克隆对象与原型对象不是同一个对象。 2、对任何对象o,都有o.clone().getClass() o.getClass()。换言之&a…

关于Clone

一般情况下,如果使用clone()方法,则需满足以下条件。
1、对任何对象o,都有o.clone() != o。换言之,克隆对象与原型对象不是同一个对象。
2、对任何对象o,都有o.clone().getClass() == o.getClass()。换言之,克隆对象与原型对象的类型一样。
3、如果对象o的equals()方法定义恰当,则o.clone().equals(o)应当成立。

我们在设计自定义类的clone()方法时,应当遵守这3个条件。一般来说,这3个条件中的前2个是必需的,第3个是可选的。

在这里插入图片描述

浅拷贝

super.clone()方法直接从堆内存中以二进制流的方式进行复制,重新分配一个内存块,因此其效率很高。
由于super.clone()方法基于内存复制,因此不会调用对象的构造函数,也就是不需要经历初始化过程。
只有基本类型的参数会被拷贝一份,非基本类型的对象不会被拷贝一份,而是继续使用传递引用的方式,原型对象与克隆对象的该属性只是指向同一对象的引用,即浅拷贝

	@Overridepublic Object clone() throws CloneNotSupportedException{Employees employees = (Employees)super.clone();return employees;}

在这里插入图片描述

深拷贝

在日常开发中,使用super.clone()方法并不能满足所有需求。如类中存在引用对象属性。就要需要实现深拷贝,必须要自己手动修改 clone 方法才行。

	@Overridepublic Object clone() throws CloneNotSupportedException{List<String> temp = new ArrayList<String>();for(String s : this.getEmpList()){temp.add(s);}return new Employees(temp);}

在这里插入图片描述

使用序列化(Serializable)

不过如果当原型对象维护很多引用属性的时候,手动分配会比较烦琐。因此,在Java中,如果想完成原型对象的深克隆,则通常使用 序列化(Serializable)的方式。

public class Employees implements Cloneable,Serializable{private List<String> empList;public Employees(){empList = new ArrayList<String>();}public Employees(List<String> list){this.empList=list;}public void loadData(){//read all employees from database and put into the listempList.add("Pankaj");empList.add("Raj");empList.add("David");empList.add("Lisa");}public List<String> getEmpList() {return empList;}@Overridepublic Object clone() throws CloneNotSupportedException{List<String> temp = new ArrayList<String>();for(String s : this.getEmpList()){temp.add(s);}return new Employees(temp);}public Object deepClone() throws CloneNotSupportedException{try {ByteArrayOutputStream bos = new ByteArrayOutputStream();ObjectOutputStream oos = new ObjectOutputStream(bos);oos.writeObject(this);ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());ObjectInputStream ois = new ObjectInputStream(bis);return (Employees)ois.readObject();} catch (IOException | ClassNotFoundException e) {e.printStackTrace();return null;}}
}

测试

	public static void main(String[] args) throws CloneNotSupportedException {Employees emps = new Employees();emps.loadData();Employees empsNew = (Employees) emps.deepClone();Employees empsNew1 = (Employees) emps.deepClone();List<String> list = empsNew.getEmpList();list.add("John");List<String> list1 = empsNew1.getEmpList();list1.remove("Pankaj");System.out.println("emps List: "+emps.getEmpList());System.out.println("empsNew List: "+list);System.out.println("empsNew1 List: "+list1);}emps List: [Pankaj, Raj, David, Lisa]
empsNew List: [Pankaj, Raj, David, Lisa, John]
empsNew1 List: [Raj, David, Lisa]

还原克隆破坏单例的事故现场
假设有这样一个场景,如果复制的目标对象恰好是单例对象,那会不会使单例对象被破坏呢?
当然,我们在已知的情况下肯定不会这么干,但如果发生了意外怎么办?不妨来试一下

public class A11_EagerInitializedSingletonClone implements Cloneable {private static final A11_EagerInitializedSingletonClone instance = new A11_EagerInitializedSingletonClone();// private constructor to avoid client applications to use constructorprivate A11_EagerInitializedSingletonClone() {}public static A11_EagerInitializedSingletonClone getInstance() {return instance;}@Overridepublic A11_EagerInitializedSingletonClone clone() throws CloneNotSupportedException{A11_EagerInitializedSingletonClone employees = (A11_EagerInitializedSingletonClone)super.clone();return employees;}
}
A11_EagerInitializedSingletonClone instanceOne = A11_EagerInitializedSingletonClone.getInstance();
A11_EagerInitializedSingletonClone instanceOne2  = instanceOne.clone();
System.out.println(instanceOne==instanceOne2);

结果为false确实创建了两个不同的对象。

实际上防止复制破坏单例对象的解决思路非常简单,禁止复制便可。要么我们的单例类不实现Cloneable接口,要么我们重写clone()方法,在clone()方法中返回单例对象即可,具体代码如下

	@Overridepublic A11_EagerInitializedSingletonClone clone() throws CloneNotSupportedException{return instance;}
A11_EagerInitializedSingletonClone instanceOne = A11_EagerInitializedSingletonClone.getInstance();
A11_EagerInitializedSingletonClone instanceOne2  = instanceOne.clone();
System.out.println(instanceOne==instanceOne2);

结果为true

http://www.yayakq.cn/news/720774/

相关文章:

  • 做尽调需要用到的网站东莞保安公司联系电话
  • 做自媒体的网站名字活动设计方案模板
  • 做那个网站的小编比较好酒泉网站建设培训
  • 运城网站开发app自己做的网站背景怎么设置
  • 免费做网站报价公众号怎么做文章
  • 外贸开发网站开发建筑网325
  • 利用地图建网站连山建设局网站
  • 做淘宝优惠网站步骤青羊区建设局网站
  • 光明区建设局网站中国菲律宾热身赛
  • 乐清网站只做刷推广软件
  • 网站建设完工后在什么科目核算wordpress高级教程下载
  • 免费搭建网站哪个好做网站一般是什么工作
  • 放心的网站建设代理网站终端制作
  • 电子商务网站建设与管理的实践报告微信公众号移动网站开发
  • 朝阳企业网站建设方案女生做网站前台
  • 深圳网站建设 沙漠风网站怎么上线
  • 合肥网站建设需要多少钱怎么做响应式网站
  • seo和sem推广郑州网站优化费用
  • 网站建设信息科技手机网站一般宽度做多大的
  • 东莞 网站建设企业网站方案建设书
  • 网页设计素材网站知乎阿里云虚拟主机装WordPress
  • 北京seo公司网站建站网址不安全
  • 宜昌高端网站建设重庆网站品牌推广
  • iapp用网站做软件代码wordpress导航源码
  • 如何学好网站建设微软雅黑做网站会涉及到侵权吗
  • 手机营销网站模板免费下载免费咨询男科问题
  • 龙岩网站建设大概费用誉字号网站
  • 兰州新区网站建设济南网站制作公司哪家好
  • 南宁网络推广建站企帮手logo设计官网
  • 城乡建设主管部门官方网站智能识别图片