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

苏州市高新区建设局官方网站厦门医疗网站建设

苏州市高新区建设局官方网站,厦门医疗网站建设,域名网站电话,网站做推广要备案吗目录 1、对象的克隆 1.1 对象的浅拷贝 1.2 对象深拷贝 1、对象的克隆 1.1 对象的浅拷贝 在实际编程过程中#xff0c;我们常常要遇到这种情况#xff1a;有一个对象A#xff0c;在某一时刻A中已经包含了一些有效值#xff0c;此时可能会需要一个和A完全相同新对象B我们常常要遇到这种情况有一个对象A在某一时刻A中已经包含了一些有效值此时可能会需要一个和A完全相同新对象B并且此后对B任何改动都不会影响到A中的值也就是说A与B是两个独立的对象但B的初始值是由A对象确定的。 要满足这种需求虽然有很多途径但实现clone方法是其中最简单也是最高效的手段。 public class Person implements Cloneable {    private String name;    private int age;    public Person() {    }    public Person(String name, int age) {       this.name name;       this.age age;    }    public String getName() {       return name;    }    public void setName(String name) {       this.name name;    }    public int getAge() {       return age;    }    public void setAge(int age) {       this.age age;    }    protected Object clone() {       Object obj null;       try {          obj super.clone();       } catch (CloneNotSupportedException e) {          e.printStackTrace();       }       return obj;    }    public String toString() {       return Person [name name , age age ];    } } 注意: 该Person 类实现了Cloneable 重写了clone() 方法. public static void main(String[] args) {       Person p1 new Person(jack, 28);       System.out.println(p1);       Person p2 (Person) p1.clone();       System.out.println(p2);       // 不是同一个Person 对象.       System.out.println(p1 p2);       // 修改p1 对象属性值,不影响 p2 对象的属性值.       p1.setAge(30);       System.out.println(p1);       System.out.println(p2);    } Java的所有类都默认继承java.lang.Object类在java.lang.Object类中有一个方法clone()。JDK API的说明文档解释这个方法将返回Object对象的一个拷贝。要说明的有两点一是拷贝对象返回的是一个新对象而不是一个引用。二是拷贝对象与用new操作符返回的新对象的区别就是这个拷贝已经包含了一些原来对象的信息而不是对象的初始信息。 1.2 对象深拷贝 注意问题: 请看代码: package cn.test.gz.myclone; public class Person implements Cloneable {    private String name;    private int age;    private Address add;    public Person() {    }    public Person(String name, int age, Address add) {       this.name name;       this.age age;       this.add add;    }    public String getName() {       return name;    }    public void setName(String name) {       this.name name;    }    public int getAge() {       return age;    }    public void setAge(int age) {       this.age age;    }    public Address getAdd() {       return add;    }    public void setAdd(Address add) {       this.add add;    }    protected Object clone() {       Object obj null;       try {          obj super.clone();       } catch (CloneNotSupportedException e) {          e.printStackTrace();       }       return obj;    }    Override    public String toString() {       return Person [name name , age age , add: add ];    } } package cn.test.gz.myclone; public class Address {    private String country;    private String city;    public Address() {    }    public Address(String country, String city) {       this.country country;       this.city city;    }    public String getCountry() {       return country;    }    public void setCountry(String country) {       this.country country;    }    public String getCity() {       return city;    }    public void setCity(String city) {       this.city city;    }    public String toString() {       return country country , city city;    } } 注意: Address add new Address(中国, 广州);       Person p1 new Person(jack, 28, add);       add.setCountry(美国);       System.out.println(p1);       Person p2 (Person) p1.clone();       System.out.println(p2);       // 克隆的是两个不同的对象       System.out.println(p1 p2);       // 但是对象内部的成员是同一个对象.       System.out.println(p1.getAdd() p2.getAdd()); 解决办法: package cn.test.gz.myclone; public class Address implements Cloneable {    private String country;    private String city;    public Address() {    }    public Address(String country, String city) {       this.country country;       this.city city;    }    public String getCountry() {       return country;    }    public void setCountry(String country) {       this.country country;    }    public String getCity() {       return city;    }    public void setCity(String city) {       this.city city;    }    public String toString() {       return country country , city city;    }    Override    public Object clone() {       Object obj null;       try {          obj super.clone();       } catch (CloneNotSupportedException e) {          // TODO Auto-generated catch block          e.printStackTrace();       }       return obj;    } } Person 类 protected Object clone() {       Person p null;       try {          p (Person) super.clone();       } catch (CloneNotSupportedException e) {          e.printStackTrace();       }       p.add (Address) add.clone();       return p;    } 当然也可以通过序列化机制来实现对象的克隆. public static void main(String[] args) throws IOException, ClassNotFoundException {       Address add new Address(中国, 广州);       Person p1 new Person(jack, 28, add);       ObjectOutputStream oos new ObjectOutputStream(new FileOutputStream(             c:\\person.txt));       oos.writeObject(p1);       ObjectInputStream ois new ObjectInputStream(new FileInputStream(c:\\person.txt));       Person p2(Person)ois.readObject();       System.out.println(p1p2);       System.out.println(p1.getAdd()p2.getAdd());    }
http://www.zqtcl.cn/news/853308/

相关文章:

  • 做文库类网站网站所有权 备案
  • 网站开发主要内容站长之家网站查询
  • 网站推广优化排名公司wordpress添加说说
  • 维护一个网站要多少钱企业怎么做网站
  • 怎么做兼职网站成都必去十大网红景点
  • 网站建设如何传视频教程电脑网站设计公司
  • 网站开发培训教程制作网站开发公司
  • 网站前端做报名框代码seo教育培训机构
  • 做网站要学习什么分销网站
  • 镇江市住房和城乡建设局网站常州建设网站
  • 学做美食的网站男女做暖暖到网站
  • 手机的网站建设目标刚做的网站 搜不到
  • 重庆网站建设哪里比较好呢ps怎么做网站logo
  • 网站建设五项基本原则优化关键词的公司
  • 高端网站的特点p2p网站开发的流程
  • 什么网站做外贸最好坪地网站建设公司
  • 做网站费用怎么核算没有公司 接单做网站
  • 如何建设一个优秀的电商网站wordpress注册去掉电子邮件
  • 站长工具 seo综合查询长沙高校网站制作公司
  • 杭州网站定制开发哪家好wordpress重置
  • 哈尔滨市建设安全网站火车头更新wordpress
  • 做亚马逊外国网站需要语言好吗邢台seo
  • jsp在网站开发中的优势国内哪个推广网站做的好
  • 做网站工资高吗精品资料
  • 做农业需关注什么网站热门代理项目
  • 网站开发公司营业范围照片制作视频软件app
  • 做网站怎么qq邮箱验证免费拥有wordpress
  • 校园网站建设资金来源有wordpress权重
  • 魔站网站开发wordpress 3.3.1
  • 东莞个人免费建网站网站后台管理系统 asp