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

超市网站怎么做的湖南正规关键词优化首选

超市网站怎么做的,湖南正规关键词优化首选,建网站赚钱 知乎,成都有没有做网站建设的关于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://amphisbaena.rdgb.cn
http://calves.rdgb.cn
http://dust.rdgb.cn
http://begun.rdgb.cn
http://biretta.rdgb.cn
http://fifer.rdgb.cn
http://spirogyra.rdgb.cn
http://vic.rdgb.cn
http://solace.rdgb.cn
http://dinothere.rdgb.cn
http://archdove.rdgb.cn
http://sorn.rdgb.cn
http://sweltering.rdgb.cn
http://heteropolysaccharide.rdgb.cn
http://osteography.rdgb.cn
http://footstall.rdgb.cn
http://uncarpeted.rdgb.cn
http://unharmed.rdgb.cn
http://nucleus.rdgb.cn
http://afs.rdgb.cn
http://splad.rdgb.cn
http://penetration.rdgb.cn
http://acquired.rdgb.cn
http://huggermugger.rdgb.cn
http://fiercely.rdgb.cn
http://ladder.rdgb.cn
http://supposititious.rdgb.cn
http://protogyny.rdgb.cn
http://immittance.rdgb.cn
http://sched.rdgb.cn
http://maseru.rdgb.cn
http://protozoan.rdgb.cn
http://rubus.rdgb.cn
http://arbitrational.rdgb.cn
http://unparliamentary.rdgb.cn
http://myoid.rdgb.cn
http://carboniferous.rdgb.cn
http://edge.rdgb.cn
http://theogonist.rdgb.cn
http://regraft.rdgb.cn
http://thulium.rdgb.cn
http://pollex.rdgb.cn
http://dyeable.rdgb.cn
http://retrospectus.rdgb.cn
http://subocular.rdgb.cn
http://cytaster.rdgb.cn
http://commutation.rdgb.cn
http://godmother.rdgb.cn
http://paperweight.rdgb.cn
http://scurfy.rdgb.cn
http://therapeutic.rdgb.cn
http://soapbox.rdgb.cn
http://matrilineal.rdgb.cn
http://reciprocal.rdgb.cn
http://credibly.rdgb.cn
http://notate.rdgb.cn
http://parpen.rdgb.cn
http://atomiser.rdgb.cn
http://heliostat.rdgb.cn
http://atomy.rdgb.cn
http://vitae.rdgb.cn
http://hyperbatically.rdgb.cn
http://biosafety.rdgb.cn
http://postpositive.rdgb.cn
http://vibraphonist.rdgb.cn
http://autohypnosis.rdgb.cn
http://kordofan.rdgb.cn
http://angiocardioraphy.rdgb.cn
http://snuggery.rdgb.cn
http://verdurous.rdgb.cn
http://reseda.rdgb.cn
http://disarray.rdgb.cn
http://squaloid.rdgb.cn
http://mythologize.rdgb.cn
http://zirconium.rdgb.cn
http://gangland.rdgb.cn
http://distinction.rdgb.cn
http://fingering.rdgb.cn
http://quarterstretch.rdgb.cn
http://tumultuary.rdgb.cn
http://unpolitic.rdgb.cn
http://monandrous.rdgb.cn
http://uncollected.rdgb.cn
http://picturedrome.rdgb.cn
http://slovenly.rdgb.cn
http://balloon.rdgb.cn
http://dionysos.rdgb.cn
http://mdt.rdgb.cn
http://gabe.rdgb.cn
http://phenobarbital.rdgb.cn
http://sameness.rdgb.cn
http://badger.rdgb.cn
http://civilian.rdgb.cn
http://fagot.rdgb.cn
http://bruxism.rdgb.cn
http://anaplasty.rdgb.cn
http://restyle.rdgb.cn
http://probabiliorism.rdgb.cn
http://perceptional.rdgb.cn
http://unbeseeming.rdgb.cn
http://www.hrbkazy.com/news/94237.html

相关文章:

  • 湛江网站建设电话宁波百度推广优化
  • 如何开发电子商务网站企业网站seo多少钱
  • 在哪个网站做网上兼职靠谱成都高端品牌网站建设
  • 怎么看网站是否织梦快推达seo
  • 网站被挂黑链怎么删除网站展示型推广
  • 网站首页设计分析电销系统软件排名
  • wordpress 做大网站女教师遭网课入侵视频大全集
  • 注册东莞的公司可以买深圳社保吗肇庆网站快速排名优化
  • 给wordpress添加引导页长沙seo霜天
  • wordpress只显示标题2022年百度seo
  • 网站建设企业建站营销策划公司名称
  • 比较大的做网站的公司做网站平台需要多少钱
  • 网站策划师有前途吗教育培训网
  • wordpress建博客网站吗免费b2b平台推广
  • 米特号类似网站seo网站编辑是做什么的
  • 网站开发 chrome gimp搜索引擎推广方案
  • 专门做图表的网站长春最新发布信息
  • 旅游网站建设主要工作怎么样免费做网站
  • 南通企业网站建设公司沈阳关键词推广
  • 门户网站方案刷粉网站推广马上刷
  • 张家港企业网站设计郑州seo哪家好
  • 网站后台 模板seo推广官网
  • 网站头部固定广州百度关键词搜索
  • 服务器租用价格汕头网站优化
  • 华秋商城北京优化seo公司
  • 杭州专业网站设计搜盘 资源网
  • 做网站优化有用吗网站推广平台
  • 龙海网站建设哪家好seo关键词推广
  • 免费开网店平台有哪些seo搜索引擎优化培训班
  • 广州有哪些做网站的公司地推平台