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

网站实名审核中心企业文化

网站实名审核中心,企业文化,微信开放平台怎么跳过,新公司在哪做网站文章目录 前言原型模式一、浅拷贝1、案例2、引用数据类型 二、深拷贝1、重写clone()方法2、序列化 总结 前言 先看一下传统的对象克隆方式: 原型类: public class Student {private String name;public Student(String name) {this.name name;}publi…

文章目录

  • 前言
  • 原型模式
  • 一、浅拷贝
      • 1、案例
      • 2、引用数据类型
  • 二、深拷贝
      • 1、重写clone()方法
      • 2、序列化
  • 总结


前言

先看一下传统的对象克隆方式:

原型类:

public class Student {private String name;public Student(String name) {this.name = name;}public String getName() {return name;}@Overridepublic String toString() {return "Student{'name' = " + name + "}, " + "hashCode = " + this.hashCode();}
}

克隆:

@Test
public void test(){//原型对象Student student = new Student("张三");//克隆对象Student student1 = new Student(student.getName());Student student2 = new Student(student.getName());Student student3 = new Student(student.getName());System.out.println("原型对象: " + student);System.out.println("克隆对象1: " + student1);System.out.println("克隆对象2: " + student2);System.out.println("克隆对象3: " + student3);
}

在这里插入图片描述

  1. 优点是比较好理解,简单易操作;
  2. 在创建新的对象时,总是需要重新获取原始对象的属性,如果创建的对象比较复杂时,效率较低;
  3. 总是需要重新初始化对象,而不是动态地获得对象运行时的状态, 不够灵活。

原型模式

  1. 原型模式(Prototype模式)是指:用原型实例指定创建对象的种类,并且通过拷贝这些原型,创建新的对象;
  2. 原型模式是一种创建型设计模式,允许一个对象再创建另外一个可定制的对象,无需知道如何创建的细节;
  3. 工作原理是: 通过将一个原型对象传给那个要发动创建的对象,这个要发动创建的对象通过请求原型对象拷贝它们自己来实施创建,即 对象.clone()

用一个已经创建的实例作为原型,通过复制该原型对象来创建一个和原型对象相同的新对象。

原型模式包含如下角色:

  • 抽象原型类:规定了具体原型对象必须实现的的 clone() 方法。
  • 具体原型类:实现抽象原型类的 clone() 方法,它是可被复制的对象。
  • 访问类:使用具体原型类中的 clone() 方法来复制新的对象。

在这里插入图片描述
原型模式的克隆分为浅克隆和深克隆。

  • 浅克隆:创建一个新对象,新对象的属性和原来对象完全相同,对于非基本类型属性,仍指向原有属性所指向的对象的内存地址。
  • 深克隆:创建一个新对象,属性中引用的其他对象也会被克隆,不再指向原有对象地址。

一、浅拷贝

1、案例

对于上文中的克隆方法加以改进:

原型类:

public class Student implements Cloneable {private String name;public Student(String name) {System.out.println("原型对象创建成功!!!");this.name = name;}public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "Student{'name' = " + name + "}, " + "hashCode = " + this.hashCode();}//实现对象克隆@Overrideprotected Object clone() throws CloneNotSupportedException {System.out.println("克隆成功!!!");return super.clone();}
}

测试:

@Test
public void test1() throws CloneNotSupportedException {Student newStudent = new Student("张三");Student cloneStudent = (Student) newStudent.clone();System.out.println("原型对象: " + newStudent);System.out.println("克隆对象: " + cloneStudent);
}

在这里插入图片描述

2、引用数据类型

  • 上述案例中我们可以看出克隆是克隆成功了,并且没有走构造方法,所克隆出的对象地址和原对象地址不一样,是新的对象;

  • 对于数据类型是基本数据类型的成员变量,浅拷贝会直接进行值传递,也就是将该属性值复制一份给新的对象;

  • 但是引用数据类型的成员变量,比如说成员变量是某个数组、某个类的对象等,并没有new 一个新的对象,而是进行引用传递指向原有的引用;

  • 在这种情况下,在一个对象中修改该成员变量会影响到另一个对象的该成员变量值。

我们添加原型类的成员变量:

School:

public class School {private String name;public School(String name) {this.name = name;}public String getName() {return name;}public void setName(String name) {this.name = name;}
}

Student:

public class Student implements Cloneable {private String name;private School school;public Student(String name, School school) {this.name = name;this.school = school;}public void setName(String name) {this.name = name;}public School getSchool() {return school;}@Overridepublic String toString() {return "Student{'name' = " + name + ", 'school' = " + school.getName() + "}, " +"Student.hashCode = " + this.hashCode() + ", " +"name.hashCode" + name.hashCode() + ", " +"School.hashCode = " + school.hashCode();}//实现对象克隆@Overrideprotected Object clone() throws CloneNotSupportedException {return super.clone();}
}

测试:

@Test
public void test2() throws CloneNotSupportedException {Student newStudent = new Student("张三", new School("清华"));Student cloneStudent = (Student) newStudent.clone();System.out.println("原型对象:" + newStudent);System.out.println("克隆对象:" + cloneStudent);System.out.println("=====================修改克隆对象信息========================");cloneStudent.setName("李四");cloneStudent.getSchool().setName("北大");System.out.println("修改后的原型对象:" + newStudent);System.out.println("修改后的克隆对象:" + cloneStudent);
}

在这里插入图片描述
上述案例可以看出:

  • 克隆确实产生新的对象,但是引用数据类型只是进行了引用传递;
  • 以至于我们修改了cloneStudent的学校,newStudent也随之修改了;
  • 那为什么String也是引用数据类型,cloneStudent的那么由“张三”改为“李四”,而newStudent没有呢,那是因为String不可变,传入新的,当然指向新的地址了。

二、深拷贝

  1. 复制对象的所有基本数据类型的成员变量值

  2. 为所有引用数据类型的成员变量申请存储空间,并复制每个引用数据类型成员量所引用的对象,直到该对象可达的所有对象。也就是说,对象进行深拷贝要对整个对象进行拷贝

  3. 深拷贝实现方式有两种
    - 重写clone方法来实现深拷贝

    - 通过对象序列化实现深拷贝(推荐)

1、重写clone()方法

  • 重写clone方法主要是在原有的克隆的基础上,将引用数据类型再进行嵌套克隆;

  • 每个被引用的类也要实现Cloneable接口,重写clone()方法;

  • 这对全新的类来说不是很难,但对已有的类进行改造时,需要修改其源代码,违背开闭原则。

School:

public class School implements Cloneable{private String name;public School(String name) {this.name = name;}public String getName() {return name;}public void setName(String name) {this.name = name;}@Overrideprotected Object clone() throws CloneNotSupportedException {return super.clone();}
}

Student:

public class Student implements Cloneable {private String name;private School school;public Student(String name, School school) {this.name = name;this.school = school;}public void setName(String name) {this.name = name;}public School getSchool() {return school;}@Overridepublic String toString() {return "Student{'name' = " + name + ", 'school' = " + school.getName() + "}, " +"Student.hashCode = " + this.hashCode() + ", " +"name.hashCode" + name.hashCode() + ", " +"School.hashCode = " + school.hashCode();}//实现对象克隆@Overrideprotected Object clone() throws CloneNotSupportedException {//克隆基本数据类型以及StringStudent student = (Student) super.clone();//引用数据类型再进行克隆student.school = (School) student.getSchool().clone();return student;}
}

测试:

@Test
public void test3() throws CloneNotSupportedException {Student newStudent = new Student("张三", new School("清华"));Student cloneStudent = (Student) newStudent.clone();System.out.println("原型对象:" + newStudent);System.out.println("克隆对象:" + cloneStudent);System.out.println("=====================修改克隆对象信息========================");cloneStudent.setName("李四");cloneStudent.getSchool().setName("北大");System.out.println("修改后的原型对象:" + newStudent);System.out.println("修改后的克隆对象:" + cloneStudent);
}

在这里插入图片描述

2、序列化

涉及到的所有类必须实现Serializable接口,否则会抛NotSerializableException异常。

School:

public class School implements Serializable{private String name;public School(String name) {this.name = name;}public String getName() {return name;}public void setName(String name) {this.name = name;}
}

Student:

public class Student implements Serializable {private String name;private School school;public Student(String name, School school) {this.name = name;this.school = school;}public void setName(String name) {this.name = name;}public School getSchool() {return school;}@Overridepublic String toString() {return "Student{'name' = " + name + ", 'school' = " + school.getName() + "}, " +"Student.hashCode = " + this.hashCode() + ", " +"name.hashCode" + name.hashCode() + ", " +"School.hashCode = " + school.hashCode();}public Student deepClone() {ByteArrayOutputStream bos = null;ObjectOutputStream oos = null;ByteArrayInputStream bis = null;ObjectInputStream ois = null;try {//序列化bos = new ByteArrayOutputStream();oos = new ObjectOutputStream(bos);oos.writeObject(this);//反序列化bis = new ByteArrayInputStream(bos.toByteArray());ois = new ObjectInputStream(bis);return (Student) ois.readObject();} catch (Exception e) {e.printStackTrace();return null;} finally {try {if (bos != null) bos.close();if (oos != null) oos.close();if (bis != null) bis.close();if (ois != null) ois.close();} catch (IOException e) {e.printStackTrace();}}}
}

测试:

@Test
public void test4() throws CloneNotSupportedException {Student newStudent = new Student("张三", new School("清华"));Student cloneStudent = newStudent.deepClone();System.out.println("原型对象:" + newStudent);System.out.println("克隆对象:" + cloneStudent);System.out.println("=====================修改克隆对象信息========================");cloneStudent.setName("李四");cloneStudent.getSchool().setName("北大");System.out.println("修改后的原型对象:" + newStudent);System.out.println("修改后的克隆对象:" + cloneStudent);
}

在这里插入图片描述


总结

原型模式的注意事项和细节:

  1. 创建新的对象比较复杂时,可以利用原型模式简化对象的创建过程,同时也能够提高效率;
  2. 不用重新初始化对象,而是动态地获得对象运行时的状态;
  3. 如果原始对象发生变化(增加或者减少属性),其它克隆对象的也会发生相应的变化,无需修改代码;
  4. 需要注意浅拷贝的成员变量数据类型是引用数据类型(对象)的时候;
  5. 在实现深克隆的时候可能需要比较复杂的代码建议使用序列化方式;

文章转载自:
http://sublattice.jftL.cn
http://gazelle.jftL.cn
http://intending.jftL.cn
http://gaur.jftL.cn
http://visceral.jftL.cn
http://indent.jftL.cn
http://masjid.jftL.cn
http://sarcoadenoma.jftL.cn
http://parisienne.jftL.cn
http://unsicker.jftL.cn
http://sincerity.jftL.cn
http://embryotic.jftL.cn
http://latria.jftL.cn
http://marchland.jftL.cn
http://workmanlike.jftL.cn
http://leachy.jftL.cn
http://sool.jftL.cn
http://bizonia.jftL.cn
http://shandrydan.jftL.cn
http://paddlefish.jftL.cn
http://awhile.jftL.cn
http://delafossite.jftL.cn
http://markup.jftL.cn
http://tolerant.jftL.cn
http://phanerogam.jftL.cn
http://neuroendocrinology.jftL.cn
http://bere.jftL.cn
http://nonscheduled.jftL.cn
http://gemmule.jftL.cn
http://thermotherapy.jftL.cn
http://ips.jftL.cn
http://vindaloo.jftL.cn
http://monocyte.jftL.cn
http://tai.jftL.cn
http://gripple.jftL.cn
http://frangipane.jftL.cn
http://prominence.jftL.cn
http://aggravating.jftL.cn
http://shimmey.jftL.cn
http://avianize.jftL.cn
http://pneumatolysis.jftL.cn
http://salaam.jftL.cn
http://hyperuricaemia.jftL.cn
http://paradigm.jftL.cn
http://saltimbocca.jftL.cn
http://chloral.jftL.cn
http://fossiliferous.jftL.cn
http://foxiness.jftL.cn
http://trespass.jftL.cn
http://capillarity.jftL.cn
http://maintopsail.jftL.cn
http://seignorage.jftL.cn
http://eib.jftL.cn
http://ecumenical.jftL.cn
http://obsequies.jftL.cn
http://aglossia.jftL.cn
http://polyidrosis.jftL.cn
http://cpi.jftL.cn
http://chymopapain.jftL.cn
http://locknut.jftL.cn
http://backvelder.jftL.cn
http://harvest.jftL.cn
http://bondman.jftL.cn
http://auricula.jftL.cn
http://semitragic.jftL.cn
http://altimeter.jftL.cn
http://inn.jftL.cn
http://modulation.jftL.cn
http://mum.jftL.cn
http://benignantly.jftL.cn
http://distillatory.jftL.cn
http://namesake.jftL.cn
http://dma.jftL.cn
http://giddily.jftL.cn
http://microfarad.jftL.cn
http://wight.jftL.cn
http://roust.jftL.cn
http://revest.jftL.cn
http://foredo.jftL.cn
http://epsomite.jftL.cn
http://bullwork.jftL.cn
http://cilia.jftL.cn
http://palmatifid.jftL.cn
http://filipin.jftL.cn
http://deflagration.jftL.cn
http://listener.jftL.cn
http://levelly.jftL.cn
http://scytheman.jftL.cn
http://between.jftL.cn
http://stravage.jftL.cn
http://underinflated.jftL.cn
http://handcuff.jftL.cn
http://tithonia.jftL.cn
http://rippling.jftL.cn
http://lacteous.jftL.cn
http://norseland.jftL.cn
http://ballast.jftL.cn
http://caitiff.jftL.cn
http://macadamize.jftL.cn
http://jackstay.jftL.cn
http://www.dt0577.cn/news/128992.html

相关文章:

  • 网站开发工具 枫子科技设计公司排名
  • 做移动网站排名软件软文怎么写吸引人
  • 大兴高端网站建设竞价推广招聘
  • 网站建设方案书写旺道营销软件
  • 最新网站推广哪家好赣州seo公司
  • 青岛建设集团招工信息网站网络营销策划的目的
  • 国家建设工程造价数据监测平台在哪个网站学开网店哪个培训机构好正规
  • 织梦网站地图html怎么做武汉百度seo排名
  • 装饰设计图片seo和竞价排名的区别
  • 做ppt的网站 知乎普通话的顺口溜6句
  • 有什么php网站聊石家庄seo
  • 为每个中小学建设网站百度开户公司
  • 软件测试自学常用的seo工具的是有哪些
  • 做故障风的头像的网站福州百度快照优化
  • python怎么做专门的手机网站市场营销策划包括哪些内容
  • wordpress标签库网站优化排名服务
  • 在哪里进行网站域名的实名认证爱站在线关键词挖掘
  • 看设计作品的网站软件网推是干什么的
  • 代网站备案费用吗免费b站推广网站详情
  • 扬中网站建设开发上海专业seo排名优化
  • 合肥市建设委员会网站网络seo排名
  • 高端网站建设webbj汕头网站建设方案外包
  • 2017电商网站建设背景成人短期培训学校
  • 吴忠市住房和城乡建设局网站网络软文是什么
  • 关于加强网站建设的建议海南百度竞价排名
  • 网上推广平台app企业网站优化的三层含义
  • 文字游戏做的最好的网站谷歌seo视频教程
  • 如何小企业网站建设巩义网络推广
  • 广州网站优化关键词公司搜索引擎优化排名技巧
  • 建站公司 长沙和西安广州seo顾问服务