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

网站建设商新会网站设计

网站建设商,新会网站设计,深圳市专业做网站,免费建网站软件哪个好目录 1.引例2.Object中clone方法的实现3.Cloneable接口讲解4.深拷贝和浅拷贝4.1浅拷贝4.2深拷贝 1.引例 Java 中内置了一些很有用的接口, Clonable 就是其中之一. Object 类中存在一个 clone 方法, 调用这个方法可以创建一个对象的 “拷贝”. 但是要想合法调用 clone 方法。必… 目录 1.引例2.Object中clone方法的实现3.Cloneable接口讲解4.深拷贝和浅拷贝4.1浅拷贝4.2深拷贝 1.引例 Java 中内置了一些很有用的接口, Clonable 就是其中之一. Object 类中存在一个 clone 方法, 调用这个方法可以创建一个对象的 “拷贝”. 但是要想合法调用 clone 方法。必须要先实现 Clonable 接口。 否则就会抛出异常. CloneNotSupportedException 如下所示当我们要克隆一个我们已经实例化的Student类该怎么去实现呢 class Student{private int age;public Student(int age) {this.age age;}Overridepublic String toString() {return Student{ age age };} } public class Test {public static void main(String[] args) {Student student1new Student(10);} }首先我们要知道Java中所有的类都继承Object类 当我们找到Object类的原码时我们会发现一个clone()方法 那我们可能会想既然所有的类都继承Object类那当我们要克隆student1直接通过student1.clon去调用这个克隆方法来实现克隆操作但是真正去实现的时候就会发现以下问题我们找不到这个方法。 2.Object中clone方法的实现 为什么找不到Object类中的clone方法呢通过原码我们能看到Object类中clone方法由protected修饰 protected同包下可以访问不同包只有子类能访问 所以我们进行第一步方法的重写 重写后可以引用clone方法了但是我们会发现还是报错因为引用的clone方法是父类的。 当我们要将它传给子类我们就要进行第二步强制转换 强制转换后还是报错这样我们就需要进行第三步涉及异常知识后面再进行补充 public class Test {public static void main(String[] args) throws CloneNotSupportedException {Student student1new Student(10);Student student2(Student)student1.clone();System.out.println(student2.toString());} }这时解决完异常问题后当我们真正进行运行时还是会报错这样我们就要进行第四步实现Cloneable接口 完成以上四步后就可以成功完成clone了。 完整代码 class Student implements Cloneable{private int age;public Student(int age) {this.age age;}Overridepublic String toString() {return Student{ age age };}Overrideprotected Object clone() throws CloneNotSupportedException {return super.clone();} } public class Test {public static void main(String[] args) throws CloneNotSupportedException {Student student1new Student(10);Student student2(Student)student1.clone();System.out.println(student2.toString());} }输出结果 3.Cloneable接口讲解 通过原码可以发现Cloneable里面什么也没有是一个空接口也叫标记接口。 4.深拷贝和浅拷贝 4.1浅拷贝 class Money{public double money12.5; } class Student implements Cloneable{private int age;public Money mnew Money();public Student(int age) {this.age age;}Overridepublic String toString() {return Student{ age age };}Overrideprotected Object clone() throws CloneNotSupportedException {return super.clone();} } public class Test {public static void main(String[] args) throws CloneNotSupportedException {Student student1new Student(10);Student student2(Student)student1.clone();System.out.println(student2.toString());} }我们在上面的代码中加入了Money类并且在Student类中实例化了一个Money。 这时我们再进行克隆如下图所示 当我们对student1进行可能时money并没有也克隆一份这样就是浅拷贝。举例如下 我们只是改变了student2的money但是student1的money也跟着改变了。 4.2深拷贝 class Money implements Cloneable{public double money12.5;Overrideprotected Object clone() throws CloneNotSupportedException {return super.clone();} } class Student implements Cloneable{private int age;public Money mnew Money();public Student(int age) {this.age age;}Overridepublic String toString() {return Student{ age age };}Overrideprotected Object clone() throws CloneNotSupportedException {Student temp(Student)super.clone();temp.m (Money)this.m.clone();return temp;} } public class Test {public static void main(String[] args) throws CloneNotSupportedException {Student student1new Student(10);Student student2(Student)student1.clone();System.out.println(student1.m.money);System.out.println(student2.m.money);System.out.println();student2.m.money100;System.out.println(student1.m.money);System.out.println(student2.m.money);} }我们使Money类也重写clone方法并对Student类中的clone方法进行了改动来实现深拷贝。 举例如下
http://www.pierceye.com/news/475771/

相关文章:

  • 免费做文字图网站企业所得税计算方式
  • 做网站要有策划么设计师专用网站
  • 站长之家是什么哈尔滨模板建站服务商
  • 自己做网站需要备案么关键词seo资源
  • 用tornado做网站网站建设素材库
  • dedecms织梦古典艺术书画书法公司企业网站源码模板wordpress 优酷插件
  • 深圳购物网站建设301跳转wordpress
  • 如何做自己的加盟网站开发高端客户
  • 沈阳网站建设哪里好wordpress模块管理系统
  • 跨境外贸平台有哪些天津百度快速排名优化
  • 网站建设需要了解哪些方面企业的网站建设与设计论文
  • 网站建设市场分析内蒙古企业网站建设
  • 广州 网站建设公司不用代码做网站的软件
  • 本地php网站搭建wordpress前台注册登入
  • 网站做推广页需要什么软件下载豪禾创意海报设计理念
  • 河北特定网站建设推荐网站建设的技术可行性
  • 招投标 网站建设哈尔滨做网站哪好
  • 骏域网站建设专家东莞展台展馆设计搭建
  • 免费的html网站东丽手机网站建设
  • 网站建设谈客户说什么广州网站快速制作
  • 寻花问柳专注做男人喜爱的网站做网站教程 第一课
  • 个人做外贸接订单网站简道云crm
  • 小程序免费制作平台教学东莞seo关键词
  • 微网站设计平台网络营销相关的岗位有哪些
  • 手机网站建设软件有哪些内容如何做自己的加盟网站
  • 做购物平台网站 民治农业信息网站建设方案
  • 苏州网站建设找哪家东莞seo关键词排名优化排名
  • 怎么描述网站设计软件排行榜
  • 宁波网站制作优化服务公司推广找客户平台
  • 个人网站 域名选择在线画图网页版