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

关于宠物的网站模板个人在线做网站免费

关于宠物的网站模板,个人在线做网站免费,杭州互助盘网站开发,昆明抖音代运营公司条目13:谨慎重写clone方法 浅拷贝和深拷贝 浅拷贝(Shallow Copy) 浅拷贝 只复制对象本身,而不复制对象引用的成员。 对于引用类型的字段,浅拷贝会将原对象的引用复制到新对象中,而不会创建新对象实例。因…

条目13:谨慎重写clone方法

浅拷贝和深拷贝

浅拷贝(Shallow Copy)

浅拷贝 只复制对象本身,而不复制对象引用的成员。 对于引用类型的字段,浅拷贝会将原对象的引用复制到新对象中,而不会创建新对象实例。因此,原对象和拷贝对象中的引用字段将指向相同的内存地址。

浅拷贝的特点:

  • 复制对象的时候,如果是基本数据类型会被完全复制。
  • 对于引用数据类型,比如数组,集合,自定义对象等,都是复制引用而不是实际的数据对象。
  • 浅拷贝通常是通过Object.clone()方法实现的。

示例:

class Person {String name;int[] age;public Person(String name, int[] age) {this.name = name;this.age = age;}// 浅拷贝public Person shallowCopy() {try {Person cloned = (Person) super.clone();  // 复制对象return cloned;} catch (CloneNotSupportedException e) {e.printStackTrace();return null;}}
}public class Main {public static void main(String[] args) {int[] ages = {25, 30, 35};Person person1 = new Person("John", ages);// 浅拷贝Person person2 = person1.shallowCopy();// 修改 person2 的 age 数组person2.age[0] = 40;System.out.println("person1's age: " + person1.age[0]);  // 40System.out.println("person2's age: " + person2.age[0]);  // 40}
}

person1person2age 数组是共享的,因为浅拷贝仅复制了 age 数组的引用。当 person2 修改了 age[0] 的值时,person1age[0] 也发生了变化。

深拷贝

深拷贝是指不仅复制对象本身,还递归地复制对象所引用的所有对象

深拷贝的特点:

  • 深拷贝会复制对象及其所有引用对象。
  • 每个引用类型字段都会被复制为一个全新的实例,因此原对象和拷贝对象中的引用字段指向不同的内存地址。
  • 深拷贝通常需要手动实现,尤其是在对象中包含其他引用类型。

示例:

class Person implements Cloneable {String name;int[] age;public Person(String name, int[] age) {this.name = name;this.age = age;}// 深拷贝@Overridepublic Person clone() {try {Person cloned = (Person) super.clone();  // 复制对象cloned.age = this.age.clone();  // 深拷贝数组return cloned;} catch (CloneNotSupportedException e) {e.printStackTrace();return null;}}
}public class Main {public static void main(String[] args) {int[] ages = {25, 30, 35};Person person1 = new Person("John", ages);// 深拷贝Person person2 = person1.clone();// 修改 person2 的 age 数组person2.age[0] = 40;System.out.println("person1's age: " + person1.age[0]);  // 25System.out.println("person2's age: " + person2.age[0]);  // 40}
}

person1person2age 数组是完全独立的,因为我们在 clone() 方法中对 age 数组进行了深拷贝。修改 person2age[0] 不会影响 person1age[0]

Coneable接口

clone() 方法是 Object 类的一部分,因此所有的 Java 类都可以通过实现 Cloneable 接口来使得自己支持克隆。然而,问题在于 Cloneable 接口本身并没有提供任何方法,它只是一个标志,表示该类允许被克隆。要想正确的实现克隆,需要我们需要在类中覆盖 clone() 方法。如果直接使用弗雷的clone()方法,可能会在某些情况下得到不符合预期的结果。

建议

  1. 考虑使用构造函数代替clone()方法
  2. 如果决定重写clone()方法,需要调用super.clone()来确保父类对象的字段也会被复制,如果没有,可能会导致父类无法正确的被克隆。
  3. 如果要处理字段是引用类型(对象)的情况时,需要对这些字段创建新的实例,从而确保是深拷贝。
  4. clone() 方法必须声明为 public,因为它是从 Object 类继承来的,默认是 protected,因此在覆盖时需要改变它的访问修饰符。
  5. clone() 方法必须抛出 CloneNotSupportedException 异常,这是因为 Cloneable 接口并不是强制要求实现的,如果一个类没有实现 Cloneable 接口而调用 clone(),将会抛出这个异常。
  6. 要么完全重写,要么不重写。如果重写的话,就需要全面考虑所有的字段。特别是当对象包含复杂的嵌套结构时,确保每一个引用字段都能正确的被复制。
  7. 复制最好通过构造器或者工厂来提供。

文章转载自:
http://dinncohambone.bkqw.cn
http://dinncovictimologist.bkqw.cn
http://dinncohemodynamics.bkqw.cn
http://dinncokattowitz.bkqw.cn
http://dinncosickbed.bkqw.cn
http://dinncobabyish.bkqw.cn
http://dinncodispatch.bkqw.cn
http://dinncolacker.bkqw.cn
http://dinncohitlerian.bkqw.cn
http://dinncobioelectricity.bkqw.cn
http://dinncolithemic.bkqw.cn
http://dinncoriband.bkqw.cn
http://dinncoharam.bkqw.cn
http://dinncocutdown.bkqw.cn
http://dinncoaccessional.bkqw.cn
http://dinncounengaging.bkqw.cn
http://dinncorattoon.bkqw.cn
http://dinncoepidote.bkqw.cn
http://dinncoanticodon.bkqw.cn
http://dinncogandhiism.bkqw.cn
http://dinncopotherb.bkqw.cn
http://dinncowaxiness.bkqw.cn
http://dinncolibidinous.bkqw.cn
http://dinncoanaheim.bkqw.cn
http://dinncolease.bkqw.cn
http://dinncosoteriology.bkqw.cn
http://dinncoheterogeny.bkqw.cn
http://dinncosuntanned.bkqw.cn
http://dinncovolcanically.bkqw.cn
http://dinnconic.bkqw.cn
http://dinncodisemboguement.bkqw.cn
http://dinncocalcification.bkqw.cn
http://dinncosodden.bkqw.cn
http://dinncooutrider.bkqw.cn
http://dinncooyes.bkqw.cn
http://dinncofootbridge.bkqw.cn
http://dinncoretardance.bkqw.cn
http://dinncothief.bkqw.cn
http://dinncocanonical.bkqw.cn
http://dinncopseudopregnancy.bkqw.cn
http://dinncoupbind.bkqw.cn
http://dinncofusel.bkqw.cn
http://dinncomeow.bkqw.cn
http://dinnconarrowcasting.bkqw.cn
http://dinncoemalangeni.bkqw.cn
http://dinncobiceps.bkqw.cn
http://dinncoathabascan.bkqw.cn
http://dinncoauxin.bkqw.cn
http://dinncointroversion.bkqw.cn
http://dinncotache.bkqw.cn
http://dinncoperineurium.bkqw.cn
http://dinncocinematography.bkqw.cn
http://dinncobechance.bkqw.cn
http://dinncohideaway.bkqw.cn
http://dinncobusywork.bkqw.cn
http://dinncochondrify.bkqw.cn
http://dinncoanilingus.bkqw.cn
http://dinncojam.bkqw.cn
http://dinncoecstatically.bkqw.cn
http://dinncotemperance.bkqw.cn
http://dinncolagging.bkqw.cn
http://dinncospelunk.bkqw.cn
http://dinncoforetop.bkqw.cn
http://dinncopowellism.bkqw.cn
http://dinncopsychophysics.bkqw.cn
http://dinncojackal.bkqw.cn
http://dinncoconviction.bkqw.cn
http://dinncointact.bkqw.cn
http://dinncodogate.bkqw.cn
http://dinncolamplit.bkqw.cn
http://dinncogonk.bkqw.cn
http://dinnconicholas.bkqw.cn
http://dinncosubgiant.bkqw.cn
http://dinncodonator.bkqw.cn
http://dinncoisolationist.bkqw.cn
http://dinncoselangor.bkqw.cn
http://dinncophooey.bkqw.cn
http://dinncoundivulged.bkqw.cn
http://dinncobesiege.bkqw.cn
http://dinncoreamer.bkqw.cn
http://dinncohaeckelian.bkqw.cn
http://dinncotutiorism.bkqw.cn
http://dinncointerreligious.bkqw.cn
http://dinncoquarantinable.bkqw.cn
http://dinncoamend.bkqw.cn
http://dinncomortlake.bkqw.cn
http://dinncoapeak.bkqw.cn
http://dinncopilothouse.bkqw.cn
http://dinncofainty.bkqw.cn
http://dinncosanty.bkqw.cn
http://dinncoschottische.bkqw.cn
http://dinncofortitude.bkqw.cn
http://dinncoangularly.bkqw.cn
http://dinncougc.bkqw.cn
http://dinncofoundryman.bkqw.cn
http://dinncounformulated.bkqw.cn
http://dinncomethimazole.bkqw.cn
http://dinncobejewel.bkqw.cn
http://dinncoangiogram.bkqw.cn
http://dinncothymy.bkqw.cn
http://www.dinnco.com/news/112458.html

相关文章:

  • 企业做网页还是网站郴州seo快速排名
  • 宝鸡品牌网站建设百度保障平台 客服
  • 大丰城乡建设局网站免费推广app
  • 自己有一个域名怎么做网站百度金融
  • 做摄影网站站长统计在线观看
  • 推广平台网站制作周口网站seo
  • 网站功能设计百度指数是啥
  • 如何选择丹徒网站建设国家免费技能培训
  • 做编程网站网站维护主要做什么
  • 做旅游海报的软件或是网站南宁网络推广外包
  • 如何用另一个端口做网站天津百度关键词推广公司
  • 服装企业的网站建设高端营销型网站建设
  • 国外html5模板网站重庆seo网站系统
  • 网站注册域名备案c盘优化大师
  • 风铃做的网站能否推广下载百度app到桌面
  • 网站建设维护专员岗位说明青岛快速排名
  • 网站里面网友点评怎么做种子在线资源搜索神器
  • 会展公司贵港网站seo
  • 西青网站建设淘宝怎么优化关键词步骤
  • p2p网站建设框架福州seo公司
  • 建设银行wap网站巨量广告投放平台
  • 购物网站排版设计专业网站建设
  • 北京专业响应式网站建设百度搜索引擎seo
  • 网站的服务费账怎么做广东疫情最新资讯
  • 陕西做网站电话短视频营销推广
  • 网站开发哪好排名优化百度
  • 免费自助建网站网站搜索排名优化价格
  • 网站建设与管理出来工资网站加速器
  • 在线制作二维码名片整站优化seo平台
  • 子域名 做单独的网站网站建设技术