图1 拷贝思维导图(点击查看图片)

1,拷贝

  有两个相同属性的对象A和B,A拥有初始化值,将其值拷贝到B中,使得B拥有与A“相同”数据的属性!注意这里的相同我有加双引号!

  相同可能表示这么几个意思:①数值相同【指向不同的内存空间】;②地址相同【指向相同的内存空间】;

  下面是直接使用"="进行复制的操作

 package com.cnblogs.mufasa.Demo1;

 import java.util.Date;

 class Person{//
private int age=0;
private Birth birth=new Birth();
private String name=""; public Person(int age, Birth birth, String name) {
this.age = age;
this.birth = birth;
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public Birth getBirth() {
return birth;
} public void setBirth(Birth birth) {
this.birth = birth;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
public String toString(){
return "姓名:"+name+",年龄:"+age+",出生日期:"+birth.toString();
}
} class Birth{//生日
private String date="";
public Birth(){}
public Birth(String date){
this.date=date;
} public String getDate() {
return date;
} public void setDate(String date) {
this.date = date;
} @Override
public String toString() {
return this.date;
}
} public class Demo1 {//浅拷贝
public static void main(String[] args) {
Person p1=new Person(18,new Birth("19950729"),"万雨");
Person p2=p1;
System.out.println(p1.toString());
System.out.println(p2.toString()); p1.setAge(17);
p1.setBirth(new Birth("2018"));
p1.setName("Mufasa"); System.out.println(p1.toString());
System.out.println(p2.toString()); }
}
姓名:万雨,年龄:18,出生日期:19950729
姓名:万雨,年龄:18,出生日期:19950729
姓名:Mufasa,年龄:17,出生日期:2018
姓名:Mufasa,年龄:17,出生日期:2018

  其实就相当于对原始对象进行操作

2,浅拷贝

  Perosn类型对象有两个属性Age、name,将p1浅复制给p2

  基本类型直接进行数值复制,引用类型进行地址拷贝(String类型比较特殊,属于引用类型,但是它存在一个常量池需要进行特殊对待)

图2 浅拷贝

使用构造方法进行浅拷贝:

 package com.cnblogs.mufasa.Demo;

 import java.util.Date;

 class Person{//
private Age age;
private String name;
private int birth;
public Person(Age age, String name,int birth) {
this.age = age;
this.name = name;
this.birth = birth;
} public Person(Person p1){
this.age=p1.age;
this.name=p1.name;
this.birth=p1.birth;
} public int getAge() {
return this.age.getAge();
} public void setAge(int age) {
this.age.setAge(age); ;
} public void setAge(Age age) {
this.age = age;
} public int getBirth() {
return birth;
} public void setBirth(int birth) {
this.birth = birth;
} public String getName() {
return this.name;
} public void setName(String name) {
this.name = name;
}
public String toString(){
return "姓名:"+this.name+",年龄:"+this.age+",出生日期:"+this.birth;
}
} class Age{
private int age;
public Age(){}
public Age(int age){
this.age=age;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
}
public String toString(){
return this.age+"";
}
} public class Demo {//浅拷贝-构造方法实现
public static void main(String[] args) {
Person p1=new Person(new Age(18),"万雨",1995);
Person p2=new Person(p1);
System.out.println(p1.toString());
System.out.println(p2.toString()); p1.setAge(17);
p1.setBirth(2019);
p1.setName("Mufasa"); System.out.println(p1.toString());
System.out.println(p2.toString()); }
}

使用继承Cloneable接口调用clone方法进行浅拷贝

 package com.cnblogs.mufasa.Demo2;

 import java.util.Date;

 class Person implements Cloneable{//
private Age age;
private String name;
private int birth;
public Person(Age age, String name,int birth) {
this.age = age;
this.name = name;
this.birth = birth;
} public Person clone(){
Person obj=null;
try {
obj=(Person) super.clone();
}catch (CloneNotSupportedException e){
e.printStackTrace();
}
// obj.age=(Age) this.getAge().clone();
return obj;
} public Age getAge() {
return this.age;
} public void setAge(int age) {
this.age.setAge(age); ;
} public void setAge(Age age) {
this.age = age;
} public int getBirth() {
return birth;
} public void setBirth(int birth) {
this.birth = birth;
} public String getName() {
return this.name;
} public void setName(String name) {
this.name = name;
}
public String toString(){
return "姓名:"+this.name+",年龄:"+this.age+",出生日期:"+this.birth;
}
} class Age implements Cloneable{
private int age;
public Age(){}
public Age(int age){
this.age=age;
}
// public Age clone(){
// Age obj=null;
// try {
// obj=(Age) super.clone();
// }catch (CloneNotSupportedException e){
// e.printStackTrace();
// }
// return obj;
// } public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
}
public String toString(){
return this.age+"";
}
} public class Demo2 {//浅拷贝-继承Cloneable接口实现
public static void main(String[] args) {
Person p1=new Person(new Age(18),"万雨",1995);
Person p2=(Person)p1.clone();
System.out.println(p1.toString());
System.out.println(p2.toString()); p1.setAge(17);
p1.setBirth(2019);
p1.setName("Mufasa"); System.out.println(p1.toString());
System.out.println(p2.toString()); }
}
姓名:万雨,年龄:18,出生日期:1995
姓名:万雨,年龄:18,出生日期:1995
姓名:Mufasa,年龄:17,出生日期:2019
姓名:万雨,年龄:17,出生日期:1995

3,深拷贝

  引用类型数据也进行新内存开辟与幅值,

图3 深拷贝

使用继承Cloneable接口调用clone方法进行深拷贝【每个引用对象都需要使用clone方法进行拷贝】

 package com.cnblogs.mufasa.Demo2;

 import java.util.Date;

 class Person implements Cloneable{//
private Age age;
private String name;
private int birth;
public Person(Age age, String name,int birth) {
this.age = age;
this.name = name;
this.birth = birth;
} public Person clone(){
Person obj=null;
try {
obj=(Person) super.clone();
}catch (CloneNotSupportedException e){
e.printStackTrace();
}
obj.age=(Age) this.getAge().clone();
return obj;
} public Age getAge() {
return this.age;
} public void setAge(int age) {
this.age.setAge(age); ;
} public void setAge(Age age) {
this.age = age;
} public int getBirth() {
return birth;
} public void setBirth(int birth) {
this.birth = birth;
} public String getName() {
return this.name;
} public void setName(String name) {
this.name = name;
}
public String toString(){
return "姓名:"+this.name+",年龄:"+this.age+",出生日期:"+this.birth;
}
} class Age implements Cloneable{
private int age;
public Age(){}
public Age(int age){
this.age=age;
}
public Age clone(){
Age obj=null;
try {
obj=(Age) super.clone();
}catch (CloneNotSupportedException e){
e.printStackTrace();
}
return obj;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
}
public String toString(){
return this.age+"";
}
} public class Demo2 {//浅拷贝-继承Cloneable接口实现
public static void main(String[] args) {
Person p1=new Person(new Age(18),"万雨",1995);
Person p2=(Person)p1.clone();
System.out.println(p1.toString());
System.out.println(p2.toString()); p1.setAge(17);
p1.setBirth(2019);
p1.setName("Mufasa"); System.out.println(p1.toString());
System.out.println(p2.toString()); }
}

使用继承Serializable接口进行序列化与反序列化进行深拷贝【注意对象不能使用transient进行修饰,原因:transient修饰的对象无法进行序列化!】

 package com.cnblogs.mufasa.Demo3;

 import java.io.*;

 class Person implements Serializable{
private Age age;
private String name;
private int birth;
public Person(Age age, String name,int birth) {
this.age = age;
this.name = name;
this.birth = birth;
} public Age getAge() {
return this.age;
} public void setAge(int age) {
this.age.setAge(age); ;
} public void setAge(Age age) {
this.age = age;
} public int getBirth() {
return birth;
} public void setBirth(int birth) {
this.birth = birth;
} public String getName() {
return this.name;
} public void setName(String name) {
this.name = name;
}
public String toString(){
return "姓名:"+this.name+",年龄:"+this.age+",出生日期:"+this.birth;
}
} class Age implements Serializable {
private int age;
public Age(){}
public Age(int age){
this.age=age;
}
public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
}
public String toString(){
return this.age+"";
}
} public class Demo3 {//浅拷贝-继承Cloneable接口实现
public static void main(String[] args) throws IOException, ClassNotFoundException {
Person p1=new Person(new Age(18),"万雨",1995); ByteArrayOutputStream bos=new ByteArrayOutputStream();
ObjectOutputStream oos=new ObjectOutputStream(bos); oos.writeObject(p1);//1,先进行序列化
oos.flush();
ObjectInputStream ois=new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));
Person p2=(Person)ois.readObject();//2,进行反序列化 System.out.println(p1.toString());
System.out.println(p2.toString()); p1.setAge(17);
p1.setBirth(2019);
p1.setName("Mufasa"); System.out.println(p1.toString());
System.out.println(p2.toString()); }
}
姓名:万雨,年龄:18,出生日期:1995
姓名:万雨,年龄:18,出生日期:1995
姓名:Mufasa,年龄:17,出生日期:2019
姓名:万雨,年龄:18,出生日期:1995

Java浅拷贝与深拷贝(思维导图)的更多相关文章

  1. [Java 并发] Java并发编程实践 思维导图 - 第一章 简单介绍

    阅读<Java并发编程实践>一书后整理的思维导图.

  2. [Java 并发] Java并发编程实践 思维导图 - 第二章 线程安全性

    依据<Java并发编程实践>一书整理的思维导图.

  3. [Java 并发] Java并发编程实践 思维导图 - 第四章 对象的组合

    依据<Java并发编程实践>一书整理的思维导图. 第一部分: 第二部分:

  4. 学习Java的9张思维导图

    转自:https://blog.csdn.net/aitaozi11/article/details/79652943 网上搜集了java的学习思维导图,分享给大家. 01.Java程序设计(基础) ...

  5. Java建造者模式(思维导图)

    图1 建造者模式[点击查看大图] 基本的知识点已在思维导图中,下面是demo 1,Builder 为创建一个产品对象的各个部件指定抽象接口 public interface PersonBuilder ...

  6. Java组合模式(思维导图)

    图1 组合模式[点击查看图片] 1,以公司职员为例的结构 package com.cnblogs.mufasa.demo3; import java.util.ArrayList; import ja ...

  7. Java门面模式(思维导图)

    图1 门面模式[点击查看图片] 1,实体对象类 package com.cnblogs.mufasa.demo1; //3个子系统,解决问题的实体 public class StoreA { //示意 ...

  8. Java Web学习过程的思维导图

    今天找文件,无意中翻到老师前段时间总结的知识点.觉得应该有点用处,所以分享给大家. 第一次在博客园发表,如有错误,还请指正.

  9. java编程思想-泛型思维导图

  10. java编程思想-并发思维导图

随机推荐

  1. tocke - 加密

    //获取32位随机字符串 $randTocke = getRandChar($length); //当前脚本运行时间, 单位为十万分之一毫秒 $timestamp = $_SERVER['REQUES ...

  2. Sql中truncate,delete以及drop的比较

    相同点: 1.truncate和不带where子句的delete.以及drop都会删除表内的数据. 2.drop.truncate都是DDL语句(数据定义语言),执行后会自动提交. 不同点: 1. t ...

  3. Android——coredump解析

    撰写不易,转载需注明出处:http://blog.csdn.net/jscese/article/details/46916869本文来自 [jscese]的博客! coredump文件生成前文And ...

  4. [SQL]as的是否可以省略的问题

    数据库中as主要作用是起别名,常规来说都可以省略,但是为了增加可读性,不建议省略.

  5. iptables之精髓(二)

    iptables实际操作 使用-v选项后,iptables为我们展示的信息更多了,那么,这些字段都是什么意思呢?我们来总结一下 pkts:对应规则匹配到的报文的个数. bytes:对应匹配到的报文包的 ...

  6. kettle的用法

    一: 从一个数据库导入表的数据到另一个 数据库的表中(表数据同步) 1:在 主对象树-- DB连接 中新建 连接: 在选项中 设置字符集: 2: 在 核心对象中 先增加一个  表输入: 再增加一个 插 ...

  7. Jenkins+gitlab+maven持续集成

    https://blog.csdn.net/tq08g2z/article/details/79718425 https://www.jianshu.com/p/3507d8b2ac87 报错用下面解 ...

  8. XHProf报告字段含义

    Function Name:方法名称. Calls:方法被调用的次数. Calls%:方法调用次数在同级方法总数调用次数中所占的百分比. Incl.Wall Time(microsec):方法执行花费 ...

  9. swift 第八课 CollectView的 添加 footerView 、headerView

    collectView 也是 iOS 很常用的瀑布流展示控件了,虽然使用过很多次,一直没有系统的总结过,尤其是在添加header 和footer view 的时候,很常见,写起来总觉得不是很流畅,这里 ...

  10. 文件夹中含有子文件夹,修改子文件夹中的图像存储格式(python实现)

    文件夹中含有子文件夹,修改子文件夹中的图像存储格式,把png图像改为jpg图像,python代码如下: import os import cv2 filePath = 'C:\\Users\\admi ...